二叉树的基础面试题(力扣leetcode、牛客)

二叉树的进阶面试题
1. 二叉树的前序遍历

class Solution {
public:
    vector<int> arr;
    vector<int> preorderTraversal(TreeNode* root) {
        if(root==nullptr)
        {
            return arr;
        }
        arr.push_back(root->val);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
        return arr;
    }
};

2. 二叉树中序遍历

class Solution {
public:
    vector<int> arr;
    vector<int> inorderTraversal(TreeNode* root) {
        if(root==nullptr)
        {
            return arr;
        }
        inorderTraversal(root->left);
        arr.push_back(root->val);
        inorderTraversal(root->right);
        return arr;
    }
};

3. 二叉树的后序遍历

class Solution {
public:
    vector<int> arr;
    vector<int> postorderTraversal(TreeNode* root) {
        if(root==nullptr)
        {
            return arr;
        }
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        arr.push_back(root->val);
        return arr;
    }
};

4. 检查两颗树是否相同

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p==nullptr&&q==nullptr)
            return true;
        
        if(p!=nullptr&&q!=nullptr&&p->val==q->val)
            return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
        return false;
    }
};

5. 另一颗树的子树

class Solution {
public:
    bool isSametree(struct TreeNode* s, struct TreeNode* t)
    {
        if (s == NULL && t == NULL) 
            return true;
        if(s!=nullptr&&t!=nullptr&&s->val==t->val)
            return  isSametree(s->left, t->left) && isSametree(s->right, t->right);
        return false;
                                                           
    }

    bool isSubtree(struct TreeNode* s, struct TreeNode* t) {
        if (s == NULL && t == NULL) return true;
        if (s == NULL && t != NULL) return false;
        return isSametree(s, t)
            || isSubtree(s->left, t)
            || isSubtree(s->right, t);
    }
};

6. 二叉树最大深度

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==nullptr)
            return 0;
        int left=maxDepth(root->left);
        int right=maxDepth(root->right);
        return left>right?left+1:right+1;
    }
};

7. 判断一颗二叉树是否是平衡二叉树

class Solution {
public:
    int _bf;
    int _Height(TreeNode* root)
	{
		if (nullptr == root)
			return 0;

		int leftHeight = _Height(root->left);
		int rightHeight = _Height(root->right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
    bool isBalanced(TreeNode* root) {
        if (nullptr == root)
			return true;

		int leftHeight = _Height(root->left);
		int rightHeight = _Height(root->right);
        _bf=rightHeight-leftHeight;
		if (_bf < -1 || _bf > 1)
			return false;
		return isBalanced(root->left) && isBalanced(root->right);
    }
};

8. 对称二叉树

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (root==nullptr) 
            return true;
        
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        
        while(!q.empty()) {
            TreeNode* lnode = q.front(); q.pop();
            TreeNode* rbode = q.front(); q.pop();
            
            if (lnode==nullptr||rbode==nullptr) {
                if ( lnode==rbode ) 
                    continue;
                else 
                    return false;
            }
            if ( lnode->val != rbode->val ) 
                return false;
            q.push( lnode->left );
            q.push( rbode->right );
            q.push( rbode->left );
            q.push( lnode->right );            
        }
        return true;
    }
};

9. 二叉树的构建及遍历

#include<iostream>
#include<string>
using namespace std;
struct BTNode{
    BTNode *left;
    BTNode *right;
    char val;
    BTNode(char c) :val(c), left(nullptr), right(nullptr) {}
};
string str;
int i;
BTNode* createBTNode()
{
    char c = str[i++];
    if (c == '#') 
      return nullptr;
    BTNode *root = new BTNode(c);
    root->left = createBTNode();
    root->right = createBTNode();
    return root;
}
void inOrderTraversal(BTNode* root) {
    if (root==nullptr) 
      return;
    inOrderTraversal(root->left);
    cout << root->val << " ";
    inOrderTraversal(root->right);
}
int main()
{
  string s;
  while(cin>>s)
  {
    str=s;
    i=0;
    BTNode *root = createBTNode();
    inOrderTraversal(root);
    cout << endl;
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值