剑指 Offer 55 - II. 平衡二叉树

利用上一题求深度的做法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {

    Boolean res = true;


    public boolean isBalanced(TreeNode root) {
        dfs(root);
        return res;
    }

    int dfs(TreeNode root){
        if(root == null) return 0;
        int left = dfs(root.left);
        int right = dfs(root.right);
        if(Math.abs(left-right) > 1) res = false;
        return Math.max(left, right) + 1;
    }
}
上一篇:【Spring浅析】一、 BeanFactory 有啥可说的?


下一篇:数显之家快讯:深圳华强北再添科技新物种-55寸双面旋转LED导向指示牌