链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean ans = true;
public boolean isBalanced(TreeNode root) {
dfs(root);
return ans;
}
public int dfs(TreeNode root) {
if (root == null) return 0;
int left = dfs(root.left), right = dfs(root.right);
if (Math.abs(left - right) > 1) ans = false;
return Math.max(left, right) + 1;
}
}