110. Balanced Binary Tree

https://leetcode.com/problems/balanced-binary-tree/description/

解题思路:
用前序判断树的高度,然后对每一步判断其左右子树高度差是否大于1

class Solution {
boolean res = true;
public boolean isBalanced(TreeNode root) {
preorder(root);
return res;
}
public int preorder(TreeNode root){
if(root == null) return 0;
int left = preorder(root.left);
int right = preorder(root.right);
if(Math.abs(left - right) > 1) res = false;
return 1 + Math.max(left, right);
}
}

你可能感兴趣的:(110. Balanced Binary Tree)