https://leetcode.com/problems/balanced-binary-tree/
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 int max(int a,int b) 13 { 14 return a>b?a:b; 15 } 16 int getDepth(TreeNode * root,bool& flag) 17 { 18 if(root==NULL) 19 return 0; 20 int leftDep=getDepth(root->left,flag); 21 int rightDep=getDepth(root->right,flag); 22 if(rightDep-leftDep<-1||rightDep-leftDep>1) 23 flag=false; 24 int dep=max(rightDep,leftDep)+1; 25 return dep; 26 } 27 28 bool isBalanced(TreeNode* root) { 29 bool flag=true; 30 int depth=getDepth(root,flag); 31 return flag; 32 } 33 };