Leetcode:面试题55 - II. 平衡二叉树
Leetcode:面试题55 - II. 平衡二叉树
Talk is cheap . Show me the code .
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalance(TreeNode* root,int& height){
if(root==NULL){
height=0;
return true;
}
int left,right;
if(isBalance(root->left,left)&&isBalance(root->right,right)){
if(abs(left-right)<2){
height=max(left,right)+1;
return true;
}
}
return false;
}
bool isBalanced(TreeNode* root) {
int height=0;
return isBalance(root,height);
}
};