码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 110. Balanced Binary Tree

时间:2015-02-11 21:56:18      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

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.

[Solution]

 1 bool isBalanced(TreeNode *root) 
 2     {
 3         int depth;
 4         
 5         return balanceCheck(root, depth);
 6     }
 7     
 8     bool balanceCheck(TreeNode *root, int &root_depth)
 9     {
10         int left_depth, right_depth;
11         if (root == NULL)
12         {
13             root_depth = 0;
14             return true;
15         }
16         
17         if (balanceCheck(root->left, left_depth) && balanceCheck(root->right, right_depth))
18         {
19             if (abs(left_depth - right_depth) <= 1)
20             {
21                 root_depth = max(left_depth, right_depth) + 1;
22                 return true;
23             }
24         }
25             
26         return false;
27     }

 

leetcode 110. Balanced Binary Tree

标签:

原文地址:http://www.cnblogs.com/ym65536/p/4286971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!