LeetCode 110
2020.8.17
今天的每日一题。
我的通过代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
return Math.abs(getHigh(root.left)-getHigh(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);
}
int getHigh(TreeNode root) {
if(root==null) return 0;
else if(root.left==null&&root.right==null) return 1;
else {
return 1+Math.max(getHigh(root.left),getHigh(root.right));
}
}
}
难得我的答案和官方题解几乎一模一样。