随笔 - 147  文章 - 5  评论 - 6  阅读 - 81225

打印二叉树中节点的所有祖先

题目:在1棵二叉树中,假设所有节点的值都不同,给定值x,打印值为x的节点的所有祖先节点

 

解: 利用后续非递归遍历打印。后续非递归遍历中,当节点2次进栈2次出栈后要访问它,此时根据后续遍历性质,它的左右孩子都被访问过了,访问过这个节点后再下一个要访问的节点就是当前节点的父节点。代码如下

/*
     *后序遍历
 */
 
#include<iostream>
#include<cstdlib>
using namespace std;
 
typedef char Elemtype;
typedef struct node
{
    Elemtype data;
    struct node* left;
    struct node* right;
}Tree;
 
typedef struct
{
    Tree *p;
    int count;
}Node;
 
//先序建立一个树,不存在的元素都用0代替
Tree* create_tree(void)
{
    Tree* root=NULL;
    char ch;
 
    cin>>ch;
    if(ch=='0')
        return NULL;
    else if(ch=='q')
        return root;
    else
    {
            root=(Tree*)malloc(sizeof(Tree));
            root->left=NULL;
            root->right=NULL;
            root->data=ch;
            root->left=create_tree();
            root->right=create_tree();
    }
    return root;
}
 
/*建立栈*/
#define MAX 100
typedef struct
{
    Node data[MAX];
    int top;
}Stack;
 
Stack* create_stack(void)
{
    Stack* s=(Stack*)malloc(sizeof(Stack));
    if(s)
        s->top=-1;
    else
        cout<<"create stack error"<<endl;
    return s;
}
 
int empty_stack(Stack* s)
{
    if(s->top==-1)
        return 1;
    return 0;
}
 
int full_stack(Stack* s)
{
    if(s->top==MAX-1)
        return 1;
    return 0;
}
 
void push_stack(Stack* s,Node t)
{
    if(full_stack(s))
        return ;
    s->top++;
    s->data[s->top]=t;
}
 
void pop_stack(Stack* s,Node* t)
{
    if(empty_stack(s))
        return;
    *t=s->data[s->top];
    s->top=s->top-1;
}
 
int  get_stack(Stack* s,Node* t)
{
    if(empty_stack(s))
        return 0 ;
    *t=s->data[s->top];
    return 1;
}
 
void free_stack(Stack* s)
{
    free(s);
}
 
 
//后续遍历
 
void lastorder(Tree *t,char x)
{
    Tree* p=t;
    Node tempnode;
    Stack *s;
     
    s=create_stack();
    while(p!=NULL || !empty_stack(s))
    {
        if(p!=NULL)
        {
            tempnode.p=p;
            tempnode.count=0;
            push_stack(s,tempnode);
            p=p->left;
        }
        else //p==NULL
        {
            pop_stack(s,&tempnode);
            p=tempnode.p;
            if(tempnode.count==0)
            {
                tempnode.p=p;
                tempnode.count=1;
                push_stack(s,tempnode);
                p=p->right;
            }
            else
            {
                if(p->data==x)
                {
                    cout<<p->data<<" ";
                    Node ttmp;
                    if(get_stack(s,&ttmp))
                    {
                        x=ttmp.p->data;
                    }
                }
                p=NULL;
            }
        }
    }
 
    cout<<endl;
    free_stack(s);
}
 
//统计个数
int tree_count(Tree* t)
{
    if(t)
    {
        return 1+tree_count(t->left)+tree_count(t->right);
    }
    else
        return 0;
}
 
int main(void)
{
    Tree *root;
    root=create_tree();
         
        /*
          以树上例子,输入先序树ab0d00ce00f00,打印节点d的祖先节点
        */
 
    lastorder(root,'d');
    return 0;
}

  

posted on   紫金树下  阅读(2853)  评论(1编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示