求助帖!MOOC浙江大学陈越老师的数据结构课-PTA3.3
03-树3 Tree Traversals Again (25 分)
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.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
上面的就是题目了,这是关于将堆栈中序遍历的树用后序遍历一遍。我的思路是先将堆栈的树利用操作(Pop/Push)与上一遍的操作(Pop/Push)关系确定树的结构,然后将树后序遍历!
下面是我的程序代码!
class Node(object):
def __init__(self,left_child,right_child,value):
self._left_child = left_child
self._right_child = right_child
self._value = value
@property
def left_child(self):
return self._left_child
@property
def right_child(self):
return self._right_child
@property
def value(self):
return self._value
@left_child.setter
def left_child(self,value):
self._left_child = value
@right_child.setter
def right_child(self,value):
self._right_child = value
@value.setter
def value(self,value):
self._value = vaule
def BulidTree(Tree):
Tree = []
Stack = []
temp = 0
Root = 0
n = int(input())
for i in range(n):
Tree.append(0)
for i in range(2*n):
line = input().split()
if line[0] == 'Push':
indict = int(line[1])-1
Tree[indict] = Node(None,None,int(line[1]))
Stack.append(Tree[indict])
if i!= 0:
if last_operate == 'Push':
Stack[-2].left_child = int(line[1])
if last_operate == 'Pop':
temp.right_child = int(line[1])
if line[0] == 'Pop':
temp = Stack.pop(-1)
if i!= 0:
if last_operate == 'Push':
temp.left_child = -1
temp.right_child = -1
if last_operate == 'Pop':
temp.right_child = -1
last_operate = line[0]
if temp.right_child == 'None':
temp.right_child = -1
return Root,Tree
def postorder(Root,Tree):
post_res = []
if Root != -2:
post_res += postorder(Tree[Root].left_child-1,Tree)
post_res += postorder(Tree[Root].right_child-1,Tree)
post_res.append(Tree[Root].value)
return post_res
def printlist(list):
print (" ".join(str(j) for j in list))
tree = []
Root,Tree = BulidTree(tree)
post_order = postorder(Root,Tree)
printlist(post_order)
当我提交结果是,PTA出错了!!!
我在编译器测试时符合各种情况,是我理解出问题了,还是漏掉了特定情况,求大神赐教!!
啊,markdown语法用的不好,写的太丑请原谅
网友评论