/**
* 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) {
return depth(root) != -1;
}
public int depth(TreeNode root){
if (root == null) return 0;
int r = depth(root.right);
int l = depth(root.left);
return r == -1 || l == -1 || Math.abs(r-l) > 1 ? -1 : Math.max(r,l)+1;
}
}
Balanced Binary Tree
最新推荐文章于 2020-12-18 14:21:59 发布