`
blue2048
  • 浏览: 178474 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]Balanced Binary Tree - java 递归

阅读更多
/**

 * 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) {

        int b = b(root);

        if(b==-1){

            return false;

        }

        return true;

    }



    public int b(TreeNode node){

        if(node==null){

            return 0;

        }

        int lb = b(node.left);

        int rb = b(node.right);

        if(lb==-1 || rb==-1 || Math.abs(lb-rb)>1){

            return -1;

        }

        return 1+Math.max(lb,rb);

    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics