A simple C solution[Accepted]

• ``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
int order(struct TreeNode* root,bool *resultFlag)
{
if(root==NULL)
{return 0;}
if(!(*resultFlag))
{return 0;}
int leftDepth=order(root->left,resultFlag);
int rightDepth=order(root->right,resultFlag);
if(leftDepth-rightDepth>1||leftDepth-rightDepth<-1)
{*resultFlag=false;}
return (leftDepth>rightDepth?leftDepth:rightDepth)+1;
}
bool isBalanced(struct TreeNode* root) {
if(root==NULL)
{return true;}
bool *resultFlag=(bool*)malloc(sizeof(bool));
*resultFlag=true;
order(root,resultFlag);
return *resultFlag;

}
/*
bool order(struct TreeNode* root,int *depth,int *min,int *max)
{
if(root==NULL)
{return true;}
(*depth)++;
if(root->left==NULL||root->right==NULL)
{
if(*min>*depth)
{*min=*depth;}
if(*max<*depth)
{*max=*depth;}
if(*max-*min>1)
{return false;}
}
if(!order(root->left,depth,min,max)||!order(root->right,depth,min,max))
{
return false;
}
(*depth)--;
return true;
}
bool isBalanced(struct TreeNode* root) {
if(root==NULL)
{return true;}
int *depth=(int*)malloc(sizeof(int));
*depth=0;
int *min=(int*)malloc(sizeof(int));
*min=10000;
int *max=(int*)malloc(sizeof(int));
*max=0;
order(root,depth,min,max);
printf("%d/%d",*max,*min);
return *max-*min>1?false:true;
}
}*/
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.