zoukankan      html  css  js  c++  java
  • 牛客(39)平衡二叉树

    //    题目描述
    //    输入一棵二叉树,判断该二叉树是否是平衡二叉树。
    
        public boolean IsBalanced_Solution(TreeNode root) {
            if (root==null){
                return true;
            }
    //        它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
          if (Math.abs(TreeDepth(root.left)-TreeDepth(root.right))>1) {
                return false;
          }
            boolean left = IsBalanced_Solution(root.left);
            boolean right = IsBalanced_Solution(root.right);
            return  right&&left;
        }
        public int TreeDepth(TreeNode root) {
    
            if (root==null){
                return 0;
            }
    
            int left = TreeDepth(root.left);
            int right = TreeDepth(root.right);
    
            return (left>right?left:right)+1;
        }
  • 相关阅读:
    uni_app系列
    并发编程
    VUE+DRF系列
    其实我是个诗人
    中医
    linux+docker
    Mysql数据库
    爱好
    AJAX
    Django
  • 原文地址:https://www.cnblogs.com/kaibing/p/9049351.html
Copyright © 2011-2022 走看看