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

1,二叉树的前序遍历(非递归)

class Solution {
public:
    void preorderTraversal(TreeNode* root) {
        std::stack<TreeNode*> s;
        TreeNode* cur=root;
        while(cur!=NULL||!s.empty()){
            while(cur!=NULL){
                printf("%c",cur->val);
                s.push(cur);
                cur=cur->left;
            }
            TreeNode* top=s.top();
            s.pop();
            cur=top->right;
        }
    }
};

2.二叉树的中序遍历(非递归)

class Solution {
public:
    void preorderTraversal(TreeNode* root) {
        std::stack<TreeNode*> s;
        TreeNode* cur=root;
        while(cur!=NULL||!s.empty()){
            while(cur!=NULL){
                s.push(cur);
                cur=cur->left;
            }
            TreeNode* top=s.top();
            printf("%c",top->val);
            s.pop();
            cur=top->right;
        }
    }
};

3.二叉树的后序遍历(非递归)

void  PostOrder(Node* root){
	std::stack<Node*> s;
	Node* cur = root;
	Node* last = NULL;
	while (cur != NULL&&!empty()){
		while (cur != NULL){
			s.push(cur);
			cur = cur->left;
		}
		Node* top = top();
		if (top->right == NULL){
			printf("%c", top->value);
			s.pop();
			last = top;
		}
		else if (top->right == last){
			printf("%c", top->value);
			s.pop();
			last = top;
		}
		else{
			cur = cur->right;
		}
	}
}

4,分层遍历(拉线法)

#include <queue>
void LevelOrder(Node *root) {
	if (root == NULL) {
		printf("\n");
	}
	std::queue<Node *>	q;
	// 启动
	q.push(root);

	while (!q.empty()) {
		Node *front = q.front();
		q.pop();

		printf("%c ", front->value);

		if (front->left != NULL) {
			q.push(front->left);
		}

		if (front->right != NULL) {
			q.push(front->right);
		}
	}
	printf("\n");
}

5,判断完全二叉树

bool IsComplete(Node* root){
	if (root == NULL){
		printf("\n");
	}
	std::queue<Node*> q;
	q.push(root);
	while (true){
		Node* front = q.front();
		q.pop();
		if (front == NULL){
			break;
		}
		q.push(front->left);
		q.push(front->right);
	}
	while (!empty()){
		Node* front = q.front();
		q.pop();
		if (front!=NULL){
			return false;
		}
	}
	return true;
}

6,从前序和中序中构造二叉树

int Find(int* inorder,int rootvalue,int size){
    for(int i=0;i<size;++i){
        if(inorder[i]==rootvalue){
            return i;
        }
    }
    return -1;
}
struct TreeNode* buildTree2(int preorder[],int inorder[],int preorderSize){
    if(preorderSize==0){
        return NULL;
    }
    int rootvalue=preorder[0];
    int leftsize=Find(inorder,rootvalue,preorderSize);
    struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->val=preorder[0];
    root->left=buildTree2(preorder+1,inorder,leftsize);
    root->right=buildTree2(preorder+1+leftsize,inorder+1+leftsize,preorderSize-1-leftsize);
    return root;
}

struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
    return buildTree2(preorder,inorder,preorderSize);
}

7,从中序和后序中构造二叉树

int Find(int* inorder,int rootvalue ,int size){
    for(int i=0;i<size;++i){
        if(inorder[i]==rootvalue){
            return i;
        }
    }
    return -1;
}
struct TreeNode* buildTree2(int* inorder,int* postorder,int size){
    if(size==0){
        return NULL;
    }
    //左根右   左右根
    int rootvalue=postorder[size-1];
    int leftUsed=Find(inorder,rootvalue,size);
    struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->val=postorder[size-1];
    root->left=buildTree2(inorder,postorder,leftUsed);
    root->right=buildTree2(inorder+1+leftUsed,postorder+leftUsed,size-leftUsed-1);
    return root;
}
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
    return buildTree2(inorder,postorder,postorderSize);
}

 8,二叉搜索树与二叉链表

class Solution {
public:
    //非递归解法
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        std::stack<TreeNode*>  s;
        TreeNode* head=NULL;
        TreeNode* pre=NULL;
        TreeNode* cur=pRootOfTree;
        if(pRootOfTree==NULL) return NULL;
        while(cur!=NULL||!empty()){
            while(cur!=NULL){
                s.push(cur);
                cur=cur->left;
            }
               cur=s.top();
               s.pop();
               if(pre==NULL){
                   head=cur;
               }else{
                   pre->right=cur;
                   cur->left=pre;
               }
               pre=cur;
               cur=cur->right;
        }
        return head;
    }
};

9.寻找最近公共祖先 

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        
        if(root==NULL||root==p||root==q) return root;
        root->left=lowestCommonAncestor(root->left,p,q);
        root->right=lowestCommonAncestor(root->right,p,q);
        if(root->left==NULL&&root->right==NULL){
            return NULL;
        }else if(root->left!=NULL&&root->right!=NULL){
            return root;
        }else{
            return root->left==NULL? root->right:root->left;
        }    
        
    }
};

 

猜你喜欢

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