zoukankan      html  css  js  c++  java
  • 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced.

    For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.


    判断一棵树是不是平衡二叉树。

    public int height(TreeNode root) //求一个二叉树的高度
        {
            int right,left;
            if(root == null)
                return 0;
            left = height(root.left) + 1;
            right = height(root.right) + 1;
            return Math.max(left,right);
        }
        public boolean isBalanced(TreeNode root) {
            if(root == null)
                return true;
            int left = height(root.left);
            int right = height(root.right);
            int diff = left - right;  //如果一个子树高度差大于1
            if(diff < -1 || diff > 1)
                return false;
            return isBalanced(root.left) && isBalanced(root.right);
            
        }
    
  • 相关阅读:
    天网管理系统
    NSCTF web200
    程序逻辑问题
    Once More
    Guess Next Session
    上传绕过
    加了料的报错注入
    C++ GET UTF-8网页编码转换
    Android学习笔记函数
    C++ 模拟虚拟键盘按键表
  • 原文地址:https://www.cnblogs.com/wxshi/p/7862226.html
Copyright © 2011-2022 走看看