leetcode || 110、Balanced Binary Tree

problem:

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.

Hide Tags

Tree Depth-first
Search

题意:判断一棵二叉树是否为平衡二叉树

thinking:

(1)这道题以为很简单,但是仔细研究才发现,对平衡二叉树的理解有误:左右子树的最大深度(即树高)之差不超过1,

不是特指每个叶子节点的高度,是树的高度。我一开始是求出每个叶子节点的高度,再比较,这么做不符合平衡二叉树的定义

(2)准确思路是递归 每次对左右子树进行判断。

code:

错误思路:

class Solution {
    private:
        vector<int> depth;
    public:
       bool isBalanced(TreeNode *root) {
            if(root==NULL)
                return true;
            dfs(0,root);
           sort(depth.begin(),depth.end());
           if(*(depth.end()-1)-depth[0]>1)
               return false;

           else
               return true;
        }
    protected:
        void dfs(int dep,TreeNode *node)
        {
            if(node==NULL)
            {
                cout<<dep<<endl;
                depth.push_back(dep);
                return;
            }
            dep++;
            dfs(dep,node->left);
            dfs(dep,node->right);
        }
    };

正确答案:

class Solution {
public:
    bool isBalanced(TreeNode *root) {
        int depth = 0;
        return isbalance(root, depth);  

    }
    bool isbalance(TreeNode *root, int &depth)
    {
        if(root == NULL)
        {
            depth = 0;
            return true;
        }
        int ld,rd;
        if( isbalance(root->left,ld) && isbalance(root->right,rd))
        {
            if( abs(ld - rd) > 1)
            {
                return false;
            }
            depth = ld > rd ? ld + 1 : rd + 1;
            return true;
        }
    }  

};
时间: 2024-11-08 05:47:32

leetcode || 110、Balanced Binary Tree的相关文章

LeetCode题解: Balanced Binary Tree

判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/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 subtree

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. 题目解答:判断一棵给定的二叉树是不是平衡二叉树.平衡二叉树的条

leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal

problem: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:给定二叉树的中序遍历序列和后续遍历序列,构建这棵二叉树,也很经典 thinking: (1)这种类型的题,要举个例子找

leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal

problem: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:利用二叉树的前序遍历序列和中序遍历序列 构造二叉树,很经典的一道题 thinking: (1)前序遍历的顺序是:父节点-

LeetCode题解之Balanced Binary Tree

1.题目描述 2.问题分析 DFS. 3.代码 1 bool isBalanced(TreeNode* root) { 2 if (root == NULL) 3 return true; 4 5 return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); 6 7 } 8 9 int

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

LeetCode: Balanced Binary Tree [110]

[题目] 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. [题意] 判断二叉树是否是平衡二叉树 [思路] 平衡二叉树要

110.Balanced Binary Tree Leetcode解题笔记

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. 很早以前做的了  准

leetCode 110. Balanced Binary Tree 平衡二叉树

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. 题目大意: 判断一