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), left(NULL), right(NULL) {}
8: * };
9: */
10: class Solution {
11: public:
12: bool isBalanced(TreeNode* root) {
13: if (root == NULL) return true;
14: if (abs(depth(root->left) - depth(root->right)) > 1) return false;
15: return isBalanced(root->left) && isBalanced(root->right);
16: }
17: int depth(TreeNode *root) {
18: if (root == NULL) return 0;
19: return 1 + max(depth(root->left), depth(root->right));
20: }
21: };
Wednesday, August 10, 2016
110. Balanced Binary Tree
Not much to say, a classic DFS solution on tree.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment