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

    一次AC

    平衡二叉树, 左右子树都是, 且深度差在1之内


    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int depthTree(TreeNode *root)
        {
            if(!root)
                return 0;
            if(!root->left&&!root->right)
                return 1;
            else 
                return 1 + max(depthTree(root->left), depthTree(root->right));
        }
        bool isBalanced(TreeNode *root) {
            if(!root)
                return true;
            int diff_depth = depthTree(root->left) - depthTree(root->right);
            if(isBalanced(root->left)&&isBalanced(root->right)&&diff_depth>=-1&&diff_depth<=1)
                return true;
            return false;
        }
    };


    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    oracle之同义词
    oracle之序列
    oracle之视图
    oracle表空间
    oracle 闪回技术
    oracle权限管理
    oracle的undo表空间
    创建数据库之间的连接
    oracle监听
    oracle11g文件系统库迁移到ASM库上面
  • 原文地址:https://www.cnblogs.com/vintion/p/4116977.html
Copyright © 2011-2022 走看看