[LeetCode] [Balanced Binary Tree 2012-10-08 ]

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.

The key problem is that recursive func should return not only T/F to indicate
if subtree is balanced, but also the depth for parent root to decide if
left/right has the same depth.

we use the TreeNode->val to store the depth, so an initialize of val is
needed at first.

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

class
Solution {

public:

    void
ResetTree(TreeNode * root)

    {

        if(root == NULL)

        {

            return;

        }

        root->val = 0;

        ResetTree(root->left);

        ResetTree(root->right);

    }

    

    bool
treeSet = false;

    

    bool
isBalanced(TreeNode *root) {

        

        if(treeSet == false)

        {

            ResetTree(root);

            treeSet = true;

        }

        if(root == NULL)    return
true;

        

        bool
b1 = isBalanced(root->left);

        bool
b2 = isBalanced(root->right);

        if(b1 == false
|| b2 == false)

        {

            return
false;

        }

        

        int
d1 = 0;

        int
d2 = 0;

        if(root->left)

        {

            d1 = root->left->val;

        }

        if(root->right)

        {

            d2 = root->right->val;

        }

        if(d1 > d2)

        {

            root->val = d1+1;

        }

        else

        {

            root->val = d2+1;

        }

        if((d1-d2)>1 || (d1-d2)<-1)

        {

            return
false;

        }

        return
true;

    }

};

[LeetCode] [Balanced Binary Tree 2012-10-08 ],布布扣,bubuko.com

时间: 2024-10-20 02:08:33

[LeetCode] [Balanced Binary Tree 2012-10-08 ]的相关文章

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]Balanced Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于1.这实际上是AVL树的定义.首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为0.然后递归求解:树的高度 = max(左子树高度,右子树高度)+1(根节点要算上).高度计算函数实现后,递归求解每个节点的左右子树的高度差,如果有大于1的

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

LeetCode: Balanced Binary Tree 解题报告

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. Show Tags SOLU

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代码 /** * Defi

[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. 题意:二叉树中以任意节点为父结点的两棵子树的深度差不超过1,为平衡二叉

LeetCode&mdash;&mdash;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 Balanced Binary Tree (判断平衡树)

题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),

[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. 这道题以前整理过,这里就不细写了,直接贴上代码: /** * Defi