-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#100.cc
More file actions
37 lines (34 loc) · 850 Bytes
/
LeetCode#100.cc
File metadata and controls
37 lines (34 loc) · 850 Bytes
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
bool dfs(TreeNode* root, int& dep){
if(root==NULL){
dep=0;
return true;
}
int ld,rd;
bool f1 = dfs(root->left,ld);
if(f1==false) return f1;
bool f2 = dfs(root->right,rd);
if(f2==false) return f2;
if(abs(ld-rd)>1) return false;
dep = max(ld,rd)+1;
return true;
}
public:
bool isBalanced(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return true;
int depth;
return dfs(root,depth);
}
};