PAT Tree Traversals Again

Tree Traversals Again

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

题意给出先序遍历顺序的节点 然后让你打印出后序遍历

我的方法比较老实 按照先序遍历的顺序构建一棵树  再用后序遍历输出

陈越老师的办法更巧妙  没建树 因为 push进去的是先序顺序 pop出来的是中序  然后分别递归两个顺序可的后序

我不详细介绍了 大家可以去mooc看看

下面是我的C语言代码

 1 #include "stdio.h"
 2 #include "string.h"
 3 #include "stdlib.h"
 4 typedef struct     bintre
 5 {
 6     int data;
 7     struct bintre *left;
 8     struct bintre *right;
 9 }BinTree;
10 int n,flag=1;
11 BinTree *CreatTree(BinTree *head);
12 void Traversal(BinTree *head);
13 int main()
14 {
15     int i=0;
16     int a[30]={-1};
17     BinTree *head,*t;
18
19     scanf("%d ",&n);
20     head=CreatTree(head);
21     Traversal(head);
22     printf("\n");
23 }
24 void Traversal(BinTree *head)
25 {
26     if(!head)
27         return;
28     Traversal(head->left);
29     Traversal(head->right);
30     if(flag)
31     {
32         printf("%d",head->data);
33         flag=0;
34     }
35     else
36         printf(" %d",head->data);
37 }
38 BinTree *CreatTree(BinTree *head)
39 {
40     int data;
41     char flag[6];
42     if(n<=0)
43         return NULL;
44     scanf("%s",flag);
45     scanf("%d",&data);
46
47     if(strcmp(flag,"Pop")==0)
48     {
49         head=NULL;
50         return head;
51     }
52     else
53         n--;
54     head=(BinTree *)malloc(sizeof(BinTree ));
55     head->data=data;
56     head->left=CreatTree(head->left);
57     head->right=CreatTree(head->right);
58
59     return head;
60
61 }
时间: 2024-10-13 01:10:54

PAT Tree Traversals Again的相关文章

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to

PAT 1020. Tree Traversals (25)

1020. Tree Traversals (25) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary

PAT 1020 Tree Traversals

1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree

PAT Advanced 1020 Tree Traversals (25分)

1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

PAT_A1086#Tree Traversals Again

Source: PAT A1086 Tree Traversals Again (25 分) Description: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed

PAT_A1020#Tree Traversals

Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the

Tree Traversals

Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序遍历的特点递归求解,详细内容见代码 #include <iostream> #include <queue> using namespace std; struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; int p

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i