剑指offer之中序打印二叉树(非递归实现)

简介: 剑指offer之中序打印二叉树(非递归实现)

1 问题

中序打印二叉树(非递归实现),比如二叉树如下

    /*                  2
     *            3            5           
     *         1     4      2      3       
     *      3    2 1   5  1   4  2   3  

中序:按左中右来打印二叉树,结果如下

3
1
2
3
1
4
5
2
1
2
4
5
2
3
3

2 代码实现

#include <iostream>
#include <stack>
using namespace std;
typedef struct Node
{
    int value;
    struct Node* left;
    struct Node* right;
} Node;
void center_print(Node *head)
{
  if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl;
    return;
  }
  std::stack<Node *> stack;
  Node *p = head;
  while (p != NULL || !stack.empty())
  {
    while (p != NULL)
                {
        stack.push(p);
                    p = p->left;
                }
                if (!stack.empty())
                {
        p = stack.top();
                    std::cout << p->value << std::endl;
                    stack.pop();
                    p = p->right;
                }
  } 
} 
int main()
{
    /*                  2
     *            3            5           
     *         1     4      2      3       
     *      3    2 1   5  1   4  2   3   
     */
    Node head1, node1, node2, node3, node4, node5, node6;
    Node node7, node8, node9, node10, node11, node12, node13, node14;
    head1.value = 2;
    node1.value = 3;
    node2.value = 5;
    node3.value = 1;
    node4.value = 4;
    node5.value = 2;
    node6.value = 3;
    node7.value = 3;
    node8.value = 2;
    node9.value = 1;
    node10.value = 5;
    node11.value = 1;
    node12.value = 4;
    node13.value = 2;
    node14.value = 3;
    head1.left = &node1;
    head1.right = &node2;
    node1.left = &node3;
    node1.right = &node4;
    node2.left = &node5;
    node2.right = &node6;
    node3.left = &node7;
    node3.right = &node8;
    node4.left = &node9;
    node4.right = &node10;
    node5.left = &node11;
    node5.right = &node12;
    node6.left = &node13;
    node6.right = &node14;
    node7.left = NULL;
    node7.right = NULL;
    node8.left = NULL;
    node8.right =  NULL;
    node9.left = NULL;
    node9.right = NULL;
    node10.left = NULL;
    node10.right = NULL;
    node11.left = NULL;
    node11.right = NULL;
    node12.left = NULL;
    node12.right = NULL;
    node13.left = NULL;
    node13.right = NULL;
    node14.left = NULL;
    node14.right = NULL;
    center_print(&head1);
    return 0;
}


3 运行结果

2
3
1
3
2
4
1
5
5
2
1
4
3
2
3


相关文章
排序二叉树的创建及先序、中序、后序输出二叉树
排序二叉树的创建及先序、中序、后序输出二叉树
87 1
|
8月前
|
索引
leetcode106从中序与后序遍历序列构造二叉树刷题打卡
leetcode106从中序与后序遍历序列构造二叉树刷题打卡
45 0
|
7月前
|
存储 C++
【洛谷 P1305】新二叉树 题解(结构体数组+先序遍历+二叉树)
该题目要求实现一个程序,输入一棵二叉树的节点信息并输出其前序遍历结果。输入包含树的节点数`n`和每个节点的左右子节点信息,空节点用`*`表示。样例输入是一个包含6个节点的二叉树,输出前序遍历序列`abdicj`。解决方案是使用一个结构体数组存储二叉树节点,通过`add`函数建立关联,然后用`preOrder`函数进行前序遍历。AC代码提供了一个C++实现,首先读取根节点,然后构建二叉树结构,并进行前序遍历输出。
46 0
|
8月前
|
存储
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
67 0
【剑指offer】-从上往下打印二叉树-22/67
【剑指offer】-从上往下打印二叉树-22/67
|
8月前
|
存储
【剑指offer】-把二叉树打印成多行-43/67
【剑指offer】-把二叉树打印成多行-43/67
|
存储 C语言
力扣 - 102、二叉树的层序遍历(剑指Offer - 面试题32:从上到下打印二叉树)
力扣 - 102、二叉树的层序遍历(剑指Offer - 面试题32:从上到下打印二叉树)
82 1
非递归实现二叉树遍历
非递归实现二叉树遍历
58 0
leetcode236—二叉树的最近公共祖先(递归/深搜/理解)
leetcode236—二叉树的最近公共祖先(递归/深搜/理解)
|
存储
刷题之完全二叉树的权值和小字辈及根据后序和中序遍历输出先序遍历
刷题之完全二叉树的权值和小字辈及根据后序和中序遍历输出先序遍历
150 0