Title description:
Given a binary tree, determine whether it is a highly balanced binary tree.
In this question, a highly balanced binary tree is defined as:
The absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.
My solution:
/**
* 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:
int func(TreeNode* root,bool& judge){
if(root){
int a=func(root->left,judge);
int b=func(root->right,judge);
if(abs(a-b)>1) judge=false;
return a>b?a+1:b+1;
}
return 0;
}
bool isBalanced(TreeNode* root) {
bool judge=true;
func(root,judge);
return judge;
}
};