题解 | #平衡二叉树#

平衡二叉树

http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

几天没写代码,写个递归在这想了半个小时。
就递归地判断两个子树是否是平衡二叉树就玩了,递归出口条件死活想不出来。
打小就看准了我以后成不了气候

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null){
            return true;
        }
        if(Math.abs(getDepth(root.left,0)-getDepth(root.right,0))<=1){
            return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
        }
        return false;
    }
    public int getDepth(TreeNode root,int currentLength){
        if(root!=null){
            currentLength++;
            return Math.max(getDepth(root.left,currentLength),getDepth(root.right,currentLength));
        }
        return currentLength;
    }
}
全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务