/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { if(root == null) return true; if(Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) return false; return isBalanced(root.left) && isBalanced(root.right); } public int getHeight(TreeNode root){ if(root == null) return 0; return Math.max(getHeight(root.left), getHeight(root.right))+1; // Attention +1 } }
相关文章
- 12-03leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
- 12-03LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
- 12-03leetcode Binary Tree Inorder Traversal python
- 12-03leetcode [701. 二叉搜索树中的插入操作](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
- 12-03LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
- 12-03[LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- 12-03LeetCode - Minimum Depth of Binary Tree
- 12-03Leetcode#145 Binary Tree Postorder Traversal
- 12-03Codeforces 438E The Child and Binary Tree [DP,生成函数,NTT]
- 12-03[LeetCode] 162. Find Peak Element_Medium tag: Binary Search