zoukankan      html  css  js  c++  java
  • LeetCode-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.

    class Solution {
    public:
        int sub(TreeNode* root){
            if(root==NULL)return 0;
            int h1=sub(root->left);
            int h2=sub(root->right);
            if(h1==-1||h2==-1)return -1;
            if(h1-h2>=-1&&h1-h2<=1){
                return max(h1,h2)+1;
            }
            else return -1;
            
        }
        bool isBalanced(TreeNode *root) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            if(sub(root)!=-1)return true;
            else return false;
        }
    };
    View Code
  • 相关阅读:
    响应式布局
    CSS3过渡
    CSS3背景
    CSS渐变
    CSS3选择器
    CSS3
    自定义指令
    键盘修饰符
    过滤器
    v-if与v-show区别
  • 原文地址:https://www.cnblogs.com/superzrx/p/3351650.html
Copyright © 2011-2022 走看看