练习二叉树(day01)

  1. 二叉树的镜像
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 //递归交换二叉树的左右子树,当结点为空时,递归结束
	void mirror(TreeNode* root) {
        if(root){
            TreeNode*temp=root->left;
            root->left=root->right;
            root->right=temp;
            mirror(root->left);
            mirror(root->right);
        }
    }
  1. 判断是否为对称二叉树
	//方法一(递归)
	/*两个子树互为镜像当且仅当:
	两个子树的根节点值相等;
	第一棵子树的左子树和第二棵子树的右子树互为镜像,且第一棵子树的右子树和第二棵子树的左子树互为镜像;
	*/
	bool check(TreeNode*a,TreeNode*b){
        if(!a||!b)return !a&&!b;
        return a->val==b->val&&check(a->left,b->right)&&check(a->right,b->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(!root)return true;//空树
        return check(root->left,root->right);
    }
//方法二(迭代:用栈模拟递归)
bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        stack<TreeNode*> left, right;
        TreeNode *lc = root->left;
        TreeNode *rc = root->right;
        while(lc || rc || left.size())
        {
            while (lc && rc)
            {
                left.push(lc), right.push(rc);
                lc = lc->left, rc = rc->right;
            }
            if (lc || rc) return false;
            lc = left.top(), rc = right.top();
            left.pop(), right.pop();
            if (lc->val != rc->val) return false;
            lc = lc->right, rc = rc->left;
        }
        return true;
    }
  1. 树的子结构
    输入两棵二叉树 A,B,判断 B 是不是 A 的子结构,规定空树不是任何树的子结构
	bool isSame(TreeNode*a,TreeNode*b){
        if(!b)return true;
        if(!a||a->val!=b->val)return false;
        else return isSame(a->left,b->left)&&isSame(a->right,b->right);
    }
    bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
        if(!pRoot1||!pRoot2)return false;
        if(isSame(pRoot1,pRoot2))return true;
        else return hasSubtree(pRoot1->left,pRoot2)||hasSubtree(pRoot1->right,pRoot2);
    }
  1. 重建二叉树
    根据二叉树的中序+先序,中序+后序和中序+层序都可以唯一确定一棵二叉树的特性可以利用代码来重建二叉树
//中序+前序:
TreeNode* build(vector<int>& pre,int l1,int r1,vector<int>& in,int l2,int r2){
        int ans=pre[l1],k,len1,len2;
        TreeNode* tree=new TreeNode(ans);
        for(int i=l2;i<=r2;i++){
            if(in[i]==ans){
                k=i;break;
            }
        }
        len1=k-l2;
        len2=r2-k;
        if(len1){
            tree->left=build(pre,l1+1,l1+len1,in,l2,l2+len1-1);
        }
        else tree->left=NULL;
        if(len2){
            tree->right=build(pre,l1+len1+1,r1,in,l2+len1+1,r2);
        }
        else tree->right=NULL;
        return tree;
    }
    TreeNode* buildTree(vector<int>& pre, vector<int>& in) {
        int n=pre.size();
        if(n==0)return NULL;
        return build(pre,0,n-1,in,0,n-1);
    }
//中序+后序
	TreeNode* build(vector<int>& post,int l1,int r1,vector<int>& in,int l2,int r2){
        int ans=post[r1],k,len1,len2;
        TreeNode* tree=new TreeNode(ans);
        for(int i=l2;i<=r2;i++){
            if(in[i]==ans){
                k=i;break;
            }
        }
        len1=k-l2;//左子树长度
        len2=r2-k;//右子树长度
        if(len2){
            tree->right=build(post,r1-len2,r1-1,in,r2-len2+1,r2);
        }
        else tree->right=NULL;
        if(len1){
            tree->left=build(post,l1,l1+len1-1,in,l2,l2+len1-1);
        }
        else tree->left=NULL;
        
        return tree;
    }
    TreeNode* buildTree(vector<int>& post, vector<int>& in) {
        int n=post.size();
        if(n==0)return NULL;
        return build(post,0,n-1,in,0,n-1);
    }

以上两种方式重建思路类似,代码几乎一样,区别在于前序是根左右,后序是左右根,确定根结点前序是从前往后,而后序是从后往前

//中序+层序
	TreeNode* build(vector<int>& level,int l1,int r1,vector<int>& in,int l2,int r2){
        if(l2>r2)return NULL;
        int i,j;
        for(i=l1;i<=r1;i++){
            bool flag=true;
            for(j=l2;j<=r2;j++){
                if(level[i]==in[j]){
                    flag=false;
                    break;
                }
            }
            if(!flag)break;
        }
        TreeNode*root=new TreeNode(level[i]);
        root->left=build(level,l1,r1,in,l2,j-1);
        root->right=build(level,l1,r1,in,j+1,r2);
        return root;
    }
    TreeNode* buildTree(vector<int>& level, vector<int>& in) {
        int n=level.size();
        if(n==0)return NULL;
        return build(level,0,n-1,in,0,n-1);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值