Recursive C++ CODE RoSh


#1

Comment body goes here.
"/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    */

int height(TreeNode* root){
if(root == NULL) return 0;

else
return 1+ max(height(root->left), height(root->right));

}

int Solution::isBalanced(TreeNode* root) {
if(root == NULL)
return 1;

int hL, hR;

hL = height(root->left);
hR = height(root->right);

if(abs(hL-hR) <= 1 && isBalanced(root->left) && isBalanced(root->right)) 
return true;


return 0;

}
"