[Code Random Record] (continuously updated)

Remove element

int removeElement(int* nums, int numsSize, int val) {
    
    
int fast=0;
int slow=0;
for(int fast=0;fast<numsSize;fast++)
{
    
    if(nums[fast]!=val)
  {
    
    nums[slow]=nums[fast];
  slow++;
}
}
return slow;




    
}

Rotate array

void resver(int*a,int left,int right)
{
    
    while(left<right)
{
    
    int tmp=a[left];
a[left]=a[right];
a[right]=tmp;
left++;
right--;

}

}

void rotate(int* nums, int numsSize, int k) {
    
    
k%=numsSize;
resver(nums,0,numsSize-k-1);
resver(nums,numsSize-k,numsSize-1);
resver(nums,0,numsSize-1);



    
}

Remove duplicates from sorted array

int removeDuplicates(int* nums, int numsSize) {
    
    
int dev=0;
int rec=1;
while(rec<numsSize)

{
    
    if(nums[rec]==nums[dev])
{
    
    
rec++;

}
else
{
    
    dev++;
nums[dev]=nums[rec];
rec++;



}
}
return dev+1; 
}

Merge two sorted arrays

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
    
    
int end1=m-1;
int end2=n-1;
int end3=m+n-1;
while(end1>=0&&end2>=0)
{
    
    
if(nums1[end1]>nums2[end2])
{
    
    nums1[end3--]=nums1[end1--];}
else
{
    
    nums1[end3--]=nums2[end2--];}

}  
while(end2>=0)
{
    
    nums1[end3--]=nums2[end2--];}

}

Remove linked list element

struct ListNode* removeElements(struct ListNode* head, int val) {
    
    
 struct ListNode*tail;
 struct ListNode*phead;
tail=phead=(struct ListNode*)malloc(sizeof(struct ListNode));
phead->next=NULL;
struct ListNode* cur=head;
while(cur)
{
    
    if(cur->val!=val)
{
    
    tail->next=cur;
tail=tail->next;



}
cur=cur->next;




} 
tail->next=NULL;
return phead->next;
free(phead);





}

The middle node of the linked list

struct ListNode* middleNode(struct ListNode* head) {
    
    
  struct ListNode*fast=head;
  struct ListNode*slow=head;
  while(fast&&fast->next)
  {
    
    fast=fast->next->next;
  slow=slow->next;





  } 
  return slow;
  
}

The kth node from the bottom of the linked list

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    
    
    // write code here
    if(pListHead==NULL)
    return NULL;
    
struct ListNode* fast= pListHead;
struct ListNode* slow= pListHead;
int num=k;
while(num)
{
    
    if(fast==NULL)
return NULL;

    fast=fast->next;
num--;
}
while(fast)
{
    
    slow=slow->next;
fast=fast->next;
}
return slow;

}

circular linked list

bool hasCycle(struct ListNode *head) {
    
    
   if(head==NULL)
   return false;

   struct ListNode * fast=head;
   struct ListNode * slow=head;
   while(fast&&fast->next)
   {
    
    fast=fast->next->next;
   slow=slow->next;
   if(fast==slow)
   {
    
    return true;}

} 
   return false;



}

intersecting linked lists

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    
    
    int len1=0;
    int len2=0;
    struct ListNode*cur1=headA;
    struct ListNode*cur2=headB;
    while(cur1->next)
    {
    
    len1++;
    cur1=cur1->next;
    }
    while(cur2->next)
    {
    
    len2++;
    cur2=cur2->next;
    }
    if(cur1!=cur2)
    {
    
    return NULL;}
    int count=abs(len1-len2);
     struct ListNode*longlist=headA;
     struct ListNode*shortlist=headB;
     if(len2>len1)
     {
    
    longlist=headB;
     shortlist=headA;
     }
     while(count)
     {
    
    longlist=longlist->next;
     count--;
     }
     while(longlist!=shortlist)
     {
    
    longlist=longlist->next;
     shortlist=shortlist->next;
     }
     return longlist;

Circular linked list 2

struct ListNode *detectCycle(struct ListNode *head) {
    
    


struct ListNode *fast=head;
struct ListNode *slow=head;

while(fast&&fast->next)
{
    
    fast=fast->next->next;
 slow=slow->next;
if(slow==fast)
{
    
    struct ListNode *meet=slow;
while(meet!=head)
{
    
    meet=meet->next;
head=head->next;
}
return meet;
}

}
return NULL;
}

Copy of random linked list

struct Node* copyRandomList(struct Node* head) {
    
    
struct Node*cur=head;

while(cur)
{
    
    struct Node* newnode=(struct Node*)malloc(sizeof(struct Node));
if(newnode==NULL)
{
    
    perror("malloc fail");}
newnode->val=cur->val;
 newnode->next=cur->next;
 cur->next=newnode;
 cur=newnode->next;
}
cur=head;
while(cur)
{
    
    struct Node*newnode=cur->next;
struct Node* next=newnode;
if(cur->random==NULL)
{
    
    newnode->random=NULL;}
else
{
    
    newnode->random=cur->random->next;

}
cur=next->next;

}
cur=head;
struct Node*copytail=NULL;
struct Node*copyhead=NULL;
while(cur)
{
    
    struct Node*newnode=cur->next;
struct Node* next=newnode->next;
 if(copytail==NULL)
 {
    
    copytail=copyhead=newnode;}
 else
 {
    
    copytail->next=newnode;
 copytail=copytail->next;
  }
  cur->next=next;
  cur=next;
}
return copyhead;








	
}

Split of linked list

class Partition {
    
    
public:
    ListNode* partition(ListNode* pHead, int x) {
    
    
        // write code here
    ListNode*tail1;
    ListNode*tail2;
    ListNode*head1;
    ListNode*head2;
    tail1=head1=(ListNode*)malloc(sizeof(ListNode));
    tail2=head2=(ListNode*)malloc(sizeof(ListNode));
    tail1->next=NULL;
    tail2->next=NULL;
    ListNode*cur=pHead;
    while(cur)
    {
    
    if(cur->val<x)
    {
    
    tail1->next=cur;
     tail1=tail1->next;
    }
    else 
    {
    
    
        tail2->next=cur;
        tail2=tail2->next;
    }

    cur=cur->next;
    }
    tail1->next=head2->next;
    tail2->next=NULL;
       return head1->next;

    }
 
};

Reverse linked list

struct ListNode* reverseList(struct ListNode* head) {
    
    
if(head==NULL)
return NULL;
struct ListNode* n1=NULL;
struct ListNode* n2=head;

struct ListNode* n3=n2->next;
while(n2)
{
    
    n2->next=n1;
n1=n2;
n2=n3;
if(n3)
n3=n3->next;








}
return n1;








    
}

Linked list palindrome structure

 ListNode* middleNode(ListNode* head) {
    
    
   ListNode* fast = head;
    ListNode* slow = head;
    while (fast && fast->next) {
    
    
        fast = fast->next->next;
        slow = slow->next;





    }
    return slow;

}
 ListNode* reverseList( ListNode* head) {
    
    
    if (head == NULL)
        return NULL;
    ListNode* n1 = NULL;
    ListNode* n2 = head;

     ListNode* n3 = n2->next;
    while (n2) {
    
    
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if (n3)
            n3 = n3->next;








    }
    return n1;









}

class PalindromeList {
    
    
  public:
    bool chkPalindrome(ListNode* A) {
    
    

        // write code here
    ListNode*mid=middleNode(A);
    ListNode*start=reverseList(mid);
    while(start)
    {
    
    if(A->val!=start->val)
       {
    
     return false;


       }
       A=A->next;
       start=start->next;

    }
    return true;







    }
};

valid parentheses

typedef struct Stack
{
    
    
	char* a;
	int top; // 栈顶
	int capacity; // 容量
}Stack;
void StackInit(Stack* ps)
{
    
    
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, int data)
{
    
    
	assert(ps);
	if (ps->capacity == ps->top)
	{
    
    
		int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		char* newnode = (char*)realloc(ps->a,sizeof(char) * newcapcity);
		assert(newnode);
		ps->a = newnode;
		ps->capacity = newcapcity;



	}
	ps->a[ps->top] = data;
	ps->top++;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
    
    
	assert(ps);
	return ps->top == 0;





}
// 出栈
void StackPop(Stack* ps)
{
    
    
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}
// 获取栈顶元素
char StackTop(Stack* ps)
{
    
    
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];

}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
    
    
	assert(ps);
	return ps->top;

}
// 销毁栈
void StackDestroy(Stack* ps)
{
    
    
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;



}




bool isValid(char* s) {
    
    
Stack st;
StackInit(&st);
while(*s)
{
    
    if(*s=='['||*s=='{'||*s=='(')
   {
    
    StackPush(&st,*s);
   s++;
   }
 else
 {
    
    if(StackEmpty(&st))
 return false;
 char top=StackTop(&st);
 if(*s==']'&&top=='['||*s=='}'&&top=='{'||*s==')'&&top=='(')
 {
    
    
  StackPop(&st);
  s++;
 }
 else
 {
    
    return false;}

 }
}
int ret=StackEmpty(&st);
return ret;
StackDestroy(&st);
    
}

Implementing a stack using queues




typedef struct QListNode
{
    
    
	struct QListNode* next;
	int data;
}QNode;

typedef struct Queue
{
    
    
	QNode* front;
	QNode* tail;
}Queue;
void QueueInit(Queue* q)
{
    
    
	assert(q);
	q->front = q->tail = NULL;

}
// 队尾入队列
void QueuePush(Queue* q, int x)
{
    
    
	assert(q);
	
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->data =x;
	newnode->next = NULL;
	if (q->tail == NULL)
	{
    
    
		q->tail = q->front = newnode;


	}
	else
	{
    
    
		q->tail->next = newnode;
		q->tail = newnode;
		



	}
	







}
bool QueueEmpty(Queue* q)
{
    
    
	assert(q);
	return q->front == NULL;



}

// 队头出队列
void QueuePop(Queue* q)
{
    
    
	assert(q);
	assert(!QueueEmpty(q));
	if (q->front->next == NULL)
	{
    
    
		free(q->tail);
		q->tail = q->front = NULL;



	}
	else
	{
    
    
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;




	}

}
// 获取队列头部元素
int QueueFront(Queue* q)
{
    
    
	assert(q);
	assert(q->front);
	return q->front->data;


}
// 获取队列队尾元素
int QueueBack(Queue* q)
{
    
    
	assert(q);
	assert(q->tail);
	return q->tail->data;



}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
    
    
	assert(q);
	int size = 0;

	QNode* cur = q->front;
	while (cur)
	{
    
    
		size++;
		cur = cur->next;
	}
	return size;




}
// 销毁队列
void QueueDestroy(Queue* q)
{
    
    
	assert(q);
	QNode* cur = q->front;
	while (cur)
	{
    
    
		QNode* next = cur->next;
		free(cur);
		cur = next;





	}
	q->front = q->tail = NULL;




}

typedef struct {
    
    
Queue n1;
Queue n2;
} MyStack;


MyStack* myStackCreate() {
    
    
MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
if(obj==NULL)
{
    
    perror("malloc fail");}
QueueInit(&obj->n1);
QueueInit(&obj->n2);
return obj;

    
}
bool myStackEmpty(MyStack* obj) {
    
    
return  QueueEmpty(&obj->n1)&&QueueEmpty(&obj->n2);



}

void myStackPush(MyStack* obj, int x) {
    
    
    if(!QueueEmpty(&obj->n1))
    {
    
    QueuePush(&obj->n1,x);


    }
    else
    {
    
    QueuePush(&obj->n2,x);

        
    }








    
}

int myStackPop(MyStack* obj) {
    
    
    Queue* nonempty=&obj->n1;
    Queue* empty=&obj->n2;
    if(!QueueEmpty(&obj->n2))
    {
    
    nonempty=&obj->n2;
     empty=&obj->n1;}
    while(QueueSize(nonempty)>1)
    {
    
    
        QueuePush(empty,QueueFront(nonempty));
        QueuePop(nonempty);
    }
    int top=QueueFront(nonempty);
     QueuePop(nonempty);
     return top;
}

int myStackTop(MyStack* obj) {
    
    
    if(!QueueEmpty(&obj->n1))
    {
    
    return QueueBack(&obj->n1);}
else
    {
    
    return QueueBack(&obj->n2);}
     
    
}



void myStackFree(MyStack* obj) {
    
    
   QueueDestroy(&obj->n1);
   QueueDestroy(&obj->n2);
     free(obj);
    
}

Implement queue using stack







typedef struct Stack
{
    
    
	int* a;
	int top; // 栈顶
	int capacity; // 容量
}Stack;
void StackInit(Stack* ps)//初始化栈
{
    
    
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
void StackPush(Stack* ps, int data)//入栈
{
    
    
	assert(ps);
	if (ps->capacity == ps->top)
	{
    
    
		int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		int* tmp = (int*)realloc(ps->a, sizeof(int) * newcapcity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
		}
		else
		{
    
    
			ps->a = tmp;
			ps->capacity = newcapcity;



		}






	}
	ps->a[ps->top] = data;
	ps->top++;







}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
    
    
	assert(ps);
	return ps->top == 0;



}
// 出栈
void StackPop(Stack* ps)
{
    
    
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;

}
// 获取栈顶元素
int StackTop(Stack* ps)
{
    
    
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];

}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
    
    
	assert(ps);
	return ps->top;





}

// 销毁栈
void StackDestroy(Stack* ps)
{
    
    
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;

}


typedef struct {
    
    
    Stack pushst;
    Stack popst;
    
} MyQueue;


MyQueue* myQueueCreate() {
    
    
    MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));
    if(obj==NULL)
    {
    
    perror("malloc fail");}
     StackInit(&obj->pushst);
     StackInit(&obj->popst);
     return obj;

    
}
bool myQueueEmpty(MyQueue* obj) {
    
    
    return StackEmpty(&obj->pushst)&&StackEmpty(&obj->popst);
}

void myQueuePush(MyQueue* obj, int x) {
    
    

StackPush(&obj->pushst, x);


    
}

int myQueuePop(MyQueue* obj) {
    
    

   if(StackEmpty(&obj->popst))
   {
    
    while(StackSize(&obj->pushst))
   {
    
    StackPush(&obj->popst, StackTop(&obj->pushst));
    StackPop(&obj->pushst);
   }
 
   }

  int ret=StackTop(&obj->popst);
   StackPop(&obj->popst);
   return ret;
    
}

int myQueuePeek(MyQueue* obj) {
    
    
       if(StackEmpty(&obj->popst))
   {
    
    while(StackSize(&obj->pushst))
   {
    
    StackPush(&obj->popst, StackTop(&obj->pushst));
    StackPop(&obj->pushst);
   }
 
   
   





   }
	   int ret=StackTop(&obj->popst);
  // StackPop(&obj->popst);
   return ret;
}



void myQueueFree(MyQueue* obj) {
    
    
    StackDestroy(&obj->popst);
    StackDestroy(&obj->pushst);
    free(obj);

    
}

Design circular queue


typedef struct {
    
    
    int* a;
    int tail;
    int head;
    int k;


} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    
    
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (obj == NULL)
    {
    
    
        perror("malloc fail");
    }
    int* tmp = (int*)malloc(sizeof(int) * (k + 1));
    if (tmp == NULL)
    {
    
    
        perror("malloc fail");
    }
    obj->a = tmp;
    obj->tail = obj->head = 0;
    obj->k = k;
    return obj;

}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    
    
    return obj->head == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    
    
    int next = obj->tail + 1;
    if (next == obj->k + 1)

        next = 0;



    return next == obj->head;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    
    
    if (myCircularQueueIsFull(obj))
        return false;
  
    obj->a[obj->tail] = value;
    obj->tail++;
    if (obj->tail== obj->k + 1)
    {
    
    
        obj->tail = 0;
    }
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    
    
    if (myCircularQueueIsEmpty(obj))
        return false;
   
    obj->head++;
    if (obj->head== obj->k + 1)
    {
    
    
        obj->head = 0;
    }

    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    
    
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->a[obj->head];

}

int myCircularQueueRear(MyCircularQueue* obj) {
    
    
    if (myCircularQueueIsEmpty(obj))
        return -1;

    int front = obj->tail - 1;
    if (front == -1)
    {
    
    
        front = obj->k;
    }
    return obj->a[front];

}



void myCircularQueueFree(MyCircularQueue* obj) {
    
    
    free(obj->a);
    free(obj);

}

Binary tree depth

int maxDepth(struct TreeNode* root) {
    
    
if(root==NULL)
return 0;
int left=maxDepth(root->left);
int right=maxDepth(root->right);  
return left>right?left+1:right+1;
}

same tree

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    
    
    if(p==NULL&&q==NULL)
    return true;
    if(p==NULL||q==NULL)
    return false;
    if(p->val!=q->val)
    return false;
   return  isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);  
    
}

single value binary tree

bool isUnivalTree(struct TreeNode* root) {
    
    
  if(root==NULL)
  return true;
  if(root->left&&root->left->val!=root->val)
  return false;
  if(root->right&&root->right->val!=root->val)
  return  false;
  return isUnivalTree(root->left)&&isUnivalTree(root->right);  


}

Preorder traversal of binary tree

 int BinaryTreeSize(struct TreeNode* root)
 {
    
    

return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;





 }
void  aapreorderTraversal(struct TreeNode* root, int* a,int*pi) 
{
    
    if(root==NULL)
return ;
a[(*pi)++]=root->val;
aapreorderTraversal(root->left,a,pi);
aapreorderTraversal(root->right,a,pi);




}


int* preorderTraversal(struct TreeNode* root, int* returnSize) 
{
    
    * returnSize=BinaryTreeSize(root);
int*a=(int*)malloc(*returnSize*sizeof(int));
int i=0;
aapreorderTraversal(root,a,&i); 

return a;



    
}

Flip a binary tree

struct TreeNode* invertTree(struct TreeNode* root) {
    
    
    if(root==NULL)
    return NULL;
 struct TreeNode* left=invertTree(root->left);
 struct TreeNode* right=invertTree(root->right);
 root->left=right;
 root->right=left;
 return root;
    
}

another subtree


bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    
    
    if(p==NULL&&q==NULL)
    return true;
    if(p==NULL||q==NULL)
    return false;
    if(p->val!=q->val)
    return false;
   return  isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);  
    
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
    
    
if(root==NULL)
return false;
if( isSameTree(root,subRoot))
return true;
return isSubtree(root->left, subRoot)||isSubtree(root->right, subRoot);

}

Binary tree traversal


#include <stdio.h>
#include <stdlib.h>
typedef struct BinaryTreeNode {
    
    
    char data;
    struct BinaryTreeNode* left;
    struct BinaryTreeNode* right;
} BTNode;
BTNode* BinaryTreeCreate(char* a, int* pi)
{
    
    if(a[*pi]=='#')
{
    
       (*pi)++;
    return NULL;}
BTNode* newnode=(BTNode*)malloc(sizeof(BTNode));
newnode->data=a[(*pi)++];
newnode->left=BinaryTreeCreate(a,pi);
newnode->right=BinaryTreeCreate(a,pi);
return newnode;

}
void  InOrder(BTNode* root)
{
    
    if(root==NULL)
return ;
InOrder(root->left);
printf("%c ",root->data);
InOrder( root->right);





}









int main() {
    
    
    char arr[100];
    scanf("%s",arr);
    int i=0;
   BTNode*bk= BinaryTreeCreate(arr,&i);
    InOrder(bk);
}

Postorder traversal

 int BinaryTreeSize(struct TreeNode* root)
 {
    
    return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;

 }
void aapostorderTraversal(struct TreeNode* root, int*a,int*pi)
{
    
    if(root==NULL)
return ;
aapostorderTraversal(root->left,a,pi);
aapostorderTraversal(root->right,a,pi);
a[(*pi)++]=root->val;
}

int* postorderTraversal(struct TreeNode* root, int* returnSize) 
{
    
    *returnSize=BinaryTreeSize(root);
int *a=(int*)malloc(*returnSize*sizeof(int));
if(a==NULL)
{
    
    perror("malloc fail");}
int i=0;
aapostorderTraversal(root, a,&i);
return a;


    
}

inorder traversal

  int BinaryTreeSize(struct TreeNode* root)

{
    
    

    return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;

}

void aapostorderTraversal(struct TreeNode* root, int*a,int*pi)

{
    
    if(root==NULL)

return ;

aapostorderTraversal(root->left,a,pi);

a[(*pi)++]=root->val;

aapostorderTraversal(root->right,a,pi);

}





int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    
    

*returnSize=BinaryTreeSize(root);

int*a=(int*)malloc(*returnSize*sizeof(int));

if(a==NULL)

perror("malloc fail");

int i=0;

aapostorderTraversal(root,a,&i);

return a;
}

Symmetric binary tree

bool aaisSymmetric(struct TreeNode* rootleft,struct TreeNode* rootright)
{
    
    if(rootleft==NULL&&rootright==NULL)
return true;
if(rootleft==NULL||rootright==NULL)
return false;
if(rootleft->val!=rootright->val)
return false;
return aaisSymmetric(rootleft->left,rootright->right)&&aaisSymmetric( rootleft->right, rootright->left);

}




bool isSymmetric(struct TreeNode* root) {
    
    

 return    aaisSymmetric(root->left,root->right);


}

balanced binary tree

int aaisBalanced(struct TreeNode* root)
{
    
       if(root==NULL)
return 0;
   int  leftheight=aaisBalanced(root->left);
    int rightheight=aaisBalanced(root->right);
    if(leftheight==-1)
    return -1;
    if(rightheight==-1)
    return -1;
    if(abs(leftheight-rightheight)>1)
    return -1;
    else 
    return leftheight>rightheight?leftheight+1:rightheight+1; 


}


bool isBalanced(struct TreeNode* root) {
    
    
    return aaisBalanced(root)==-1?false:true;
 
    





    
}

Guess you like

Origin blog.csdn.net/yyqzjw/article/details/134809150