-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0110.cpp
More file actions
23 lines (23 loc) · 749 Bytes
/
Copy path0110.cpp
File metadata and controls
23 lines (23 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool isBalanced(TreeNode *root) {
if (!root) return true;
stack<TreeNode *> st;
st.push(root);
while (!st.empty()) {
TreeNode *root = st.top();
if (root == nullptr) {
st.pop(), root = st.top(), st.pop();
int left = root->left ? root->left->val : 0;
int right = root->right ? root->right->val : 0;
if (abs(right - left) > 1) return false;
root->val = max(left, right) + 1;
continue;
}
st.push(nullptr);
if (root->left) st.push(root->left);
if (root->right) st.push(root->right);
}
return true;
}
};