二叉树面试题(1) (leetcode)

 1,给定一个二叉树返回它的前序遍历

递归:

int* array;
int size; 
void _preorderTraversal(struct TreeNode* root){
    if(root==NULL){
        return NULL;        
    }
    array[size++]=root->val;
    _preorderTraversal(root->left);
    _preorderTraversal(root->right);
    return;
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    array=(int*)malloc(sizeof(int)*10000);
    size=0;
    _preorderTraversal(root);
    *returnSize=size;
    return array;
}

 2,给定一个二叉树返回它的前序遍历

递归:

int* array;
int size;
void _inorderTraversal(struct TreeNode* root){
    if(root==NULL){
        return NULL;
    }
    _inorderTraversal(root->left);
    array[size++]=root->val;    
    _inorderTraversal(root->right);  
    return;
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    array=(int*)malloc(sizeof(int)*10000);
    size=0;
    _inorderTraversal(root);
    *returnSize=size;
    return array;
}

3,给定一个二叉树返回它的后序遍历

递归

int* array;
int size;
void _postorderTraversal(struct TreeNode* root){
    if(root==NULL){
        return NULL;
    }
    _postorderTraversal(root->left);
    _postorderTraversal(root->right);
    array[size++]=root->val;    
    return;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    array=(int*)malloc(sizeof(int)*10000);
    size=0;
    _postorderTraversal(root);
    *returnSize=size;
    return array;
}

4,检查两课树是否相同

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

5,另一颗树的子树

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

6,二叉树的最大深度


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;
    
}

7,平衡二叉树

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;
    
}
bool isBalanced(struct TreeNode* root) {
    if(root==NULL){
        return true;
    }
    if(abs(maxDepth(root->left)-maxDepth(root->right))>1){
        return false;
    }else{
        return isBalanced(root->left)&&isBalanced(root->right);
    } 
}

8,对称二叉树

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

9,二叉树的构建和遍历

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node{
	char val;
	struct Node* left;
	struct Node* right;
}Node;
Node* CreatTree(char preorder[], int size, int* pUsed){
	if (size == 0){
		*pUsed = 0;
		return NULL;
	}
	if (*preorder == '#'){
		*pUsed = 1;
		return NULL;
	}
	Node* root = (Node*)malloc(sizeof(Node));
	root->val = preorder[0];
	int leftUsed;
	root->left = CreatTree(preorder + 1, size - 1, &leftUsed);
	int rightUsed;
	root->right = CreatTree(preorder + 1 + leftUsed, size - 1 - leftUsed, &rightUsed);
	*pUsed = 1 + leftUsed + rightUsed;
	return root;
}
void inOrder(Node* root){
	if (root==NULL){
		return;
	}
	inOrder(root->left);
	printf("%c ", root->val);
	inOrder(root->right);
}
int main(){
	char str[100];
	while (gets(str)){
		int size = strlen(str);
		int i = 0;
		Node* root = CreatTree(str, size, &i);
		inOrder(root);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43692920/article/details/89278365
今日推荐