输出中序遍历下最后一个结点

#include<stdio.h>

#include<stdlib.h>

 

typedef char datatype; //树结构体的定义

typedef struct node {

    datatype data;

    struct node *lchild, *rchild;

} bintree;

bintree *root;

 

typedef struct stack //栈的结构定义

{

    bintree *data[100];

    int tag[100];   //为栈中的每个元素设计标记,用于后序遍历

    int top; //栈顶指针

} seqstack;

 

void push(seqstack *s, bintree *t) //进栈

{

    s->data[s->top] = t;

    s->top++;

}

 

bintree *pop(seqstack *s//出栈

{

    if (s->top != 0)

    {

         s->top--;

         return (s->data[s->top]);

    }

    else

         return NULL;

}

 

bintree *createbintree() //按照前序遍历顺序建立一颗给定的二叉树(递归)

{

    char ch;

    bintree *t;

    if ((ch = getchar()) == '#')

         t = NULL;

    else {

         t = (bintree*)malloc(sizeof(bintree));

         t->data = ch;

         t->lchild = createbintree();

         t->rchild = createbintree();

    }

    return t;

}

 

void inorder1(bintree *t) //非递归实现二叉树的中序遍历

{

    seqstack s;

    s.top = 0;

    while ((t) || (s.top != 0))

    {

         if (t)

         {

             push(&s, t);

             t = t->lchild;

         }

         else

         {

             t = pop(&s);

             printf("%c", t->data);

             t = t->rchild;

         }

    }

}

 

 

bintree *last(bintree *t) //计算中序遍历的最后一个结点

{

    if (t->rchild)

    {

         while (t->rchild)

         {

             t = t->rchild;

         }

    }

    return t;

}

 

int main()

{

    bintree *tree;

    bintree *l;

    printf("根据前序遍历输入一棵二叉树:");

    tree = createbintree();

    printf("\n");

    printf("中序遍历输出二叉树:");

    inorder1(tree);

    printf("\n");

    l = last(tree);

    printf("中序遍历下最后一个结点:");

    printf("%c",l->data);

    printf("\n");

    return 0;

}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用C++求二叉树中序遍历的结果并打印可以使用递归方法实现,具体步骤如下: 1. 如果当前结点为空,返回。 2. 递归遍历左子树。 3. 输出当前结点。 4. 递归遍历右子树。 中序遍历最后一个结点即为二叉树的最右下角结点,可以在递归遍历时记录最后一个结点中序遍历的第n个结点可以在递归遍历时使用一个计数器来记录当前是第几个结点,并在遍历到第n个结点输出。 以下是示例代码: ```c++ #include <iostream> using namespace std; // 二叉树结点定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } TreeNode* getInorderLastNode(TreeNode* root) { if (root == NULL) { return NULL; } while (root->right != NULL) { root = root->right; } return root; } int getInorderNthNode(TreeNode* root, int n, int& count) { if (root == NULL) { return -1; } int res = getInorderNthNode(root->left, n, count); if (count == n) { return root->val; } count++; if (res != -1) { return res; } return getInorderNthNode(root->right, n, count); } int main() { // 构建一个二叉树 TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); // 中序遍历 inorderTraversal(root); cout << endl; // 中序遍历最后一个结点 TreeNode* lastNode = getInorderLastNode(root); cout << lastNode->val << endl; // 中序遍历的第n个结点 int n = 3; int count = 1; int nthNode = getInorderNthNode(root, n, count); cout << nthNode << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值