求二叉树的双亲结点

思路就是如果当前任一个孩子结点的值等于k,说明当前节点即为所需结点的双亲结点,通过递归实现唯一比较麻烦的是要写很多条件,不然会报错。
主要功能实现是preorder函数其他是构建和打印二叉树的函数。

#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
#define Type char

//要求1.二叉树中的结点个数2.叶子结点个数3.某结点层次4.二叉树的宽度

struct TreeNode
{
    Type val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(Type x = ' '):val(x),left(NULL),right(NULL) {}
};

class Tree {
    bool flag;
public:
    TreeNode* createFromArray(char *pre,char *in,int n)//pre存放先序序列,in存放中序序列
    {
        TreeNode *b;
        char *p;
        int k;
        if(n<=0)    return NULL;
        b = new TreeNode();
        b->val = *pre;
        for (p = in;p <in+n;p++)
        {
            if(*p == *pre)
                break;
        }
        k = p - in;
        b->left = createFromArray(pre+1,in,k);
        b->right = createFromArray(pre+k+1,p+1,n-k-1);
        return b;
    }

    void print(TreeNode *tr)
    {
        queue<TreeNode*>q;
        TreeNode* tmp;
        q.push(tr);//根节点入队
        while(!q.empty())
        {
            int width = q.size();
            for(int i = 0; i<width;i++)
            {
                tmp = q.front();
                printf("%c ",tmp->val);
                q.pop();
                if(tmp->left!=NULL)
                {
                    q.push(tmp->left);
                }
                if(tmp->right!=NULL)
                {
                    q.push(tmp->right);
                }
            }
            printf("\n");
        }
    }

    void preorder(TreeNode *Bt, Type x)
    {
        if(Bt == NULL)  return;
        if((Bt->left != NULL||Bt->right !=NULL)&& flag == false)
        {
            if(Bt->left!=NULL && Bt->left->val == x)
            {
                flag = true;
                printf("%c的双亲结点为:%c\n",x,Bt->val);
                return;
            }
            else if(Bt->right != NULL && Bt->right->val == x)
            {
                flag = true;
                printf("%c的双亲结点为:%c\n",x,Bt->val);
                return; 
            }
            else
            {
                if(Bt->left != NULL)
                {
                    preorder(Bt->left,x);
                }
                if(Bt->right != NULL)
                {
                    preorder(Bt->right,x);
                }
            }
        }
    }
};

int main()
{
    Type a[] = {'A','B','D','I','E','C','G','H'};//前序
    Type b[] = {'I','D','B','E','A','G','C','H'};//中序 
    int n = 8;
    int choose = 0;
    TreeNode *new_BT;//创建结点
    Tree BT1;//创建类
    new_BT = BT1.createFromArray(a,b,n);
    BT1.print(new_BT);
    BT1.preorder(new_BT,'G');
    return 0;
}

二叉树如图所示:
二叉树
运行结果:
结果

  • 12
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值