简体   繁体   English

二叉树的后序/前序遍历

[英]Post-order/Pre-order Traversal of Binary Tree

I have a pre-order traversal function that looks like this: 我有一个如下的遍历函数:

void listInPreOrder(node* hd){
if(hd != NULL) {
        printf("%d, ", hd->value);
        listInPreOrder(hd->left);
        listInPreOrder(hd->right);
    }
}

That in fact works, but I thought that making it post order would be as simple as this 这实际上是可行的,但我认为将其定为订购将如此简单

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPreOrder(hd->left);
        listInPreOrder(hd->right);
        printf("%d, ", hd->value);
    }
}

But unfortunately it does not work so well. 但不幸的是,它不能很好地工作。 I'm wondering how to fix this, maybe I'm doing something simple wrong. 我想知道如何解决此问题,也许我在做一些简单的错误。 Or maybe it's completely wrong. 也许这是完全错误的。

How about you change this: 你如何改变这个:

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPreOrder(hd->left);  // PRE order ???
        listInPreOrder(hd->right); // PRE order ???
        printf("%d, ", hd->value);
    }
}

to this: 对此:

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPostOrder(hd->left);
        listInPostOrder(hd->right);
        printf("%d, ", hd->value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定有序遍历和前序遍历,生成后序遍历 - Generate the post-order traversal given the in-order and pre-order traversal C递归遍历C二叉搜索树 - C Binary Search Tree pre-order traversal with recursion 意外的树遍历 - Unexpected Pre-order traversal of Tree 后序树遍历中的分段错误 - Segmentation Fault in Post-order Tree traversal 来自前序遍历的二叉搜索树和关于左右节点的信息 - Binary search tree from pre-order traversal and information about left and right nodes 从有序和后序遍历构造二叉树 - Construct Binary Tree from in-order and post-order Traversals 我的二进制搜索树的预遍历代码正在工作,但是每个元素都是指向结构的指针的堆栈如何工作? - My code for pre-order traversal of Binary Search Tree is working but how is the stack whose each element is a pointer to the structure working? C语言中的递归后序遍历和sprintf - Recursive post-order traversal and sprintf in C 在C中没有递归的二叉搜索树的顺序和前序遍历中? - In Order and Pre Order Traversal of Binary Search Tree without recursion in C? 我试图实现一棵二叉树并按预定顺序遍历它,但它仅在显示 function 中显示 0,并且创建 function 正在工作 - I tried to implement a binary tree and traverse it in pre-order but it is showing 0 only in display function, and the create function is working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM