Skip to content

Commit c104791

Browse files
authored
Create tree.cpp
1 parent 19c798f commit c104791

File tree

1 file changed

+27
-0
lines changed
  • 0____剑指offer/tree/55.tree_is_balance

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution
2+
{
3+
public:
4+
bool isBalanced(TreeNode* node,int& legth)
5+
{
6+
if (node == NULL)
7+
return true;
8+
legth += 1;
9+
int left = legth;
10+
int right = legth;
11+
if (!isBalanced(node->left, left))
12+
return false;
13+
if (!isBalanced(node->right, right))
14+
return false;
15+
int max = std::max(left, right);
16+
int min = std::min(left, right);
17+
if (max - min > 1)
18+
return false;
19+
legth = max;
20+
return true;
21+
}
22+
bool isBalanced(TreeNode* root)
23+
{
24+
int legth = 0;
25+
return isBalanced(root, legth);
26+
}
27+
};

0 commit comments

Comments
 (0)