Write down the pattern of the problem first
def preordertraversal (self, root): if root = = None:return []re = []insert root to Stack swhile s not empty:cur_root = top of Stack Ss.pop () How to handle cur_roothow to handle cur_root.lefthow to handle cur_root.right
First, we want to put the non-empty root node into the stack, in the loop to constantly infer the stack, and then deal with the top of the stack and left child nodes and the right child node point.
Let's take a look at the current stack top element and how to handle it. The first root traversal sequence is: "Root around". And every time we deal with the top of the stack, the identity actually happens to be the root. So we can directly put the root of the output or into the result container, then the left child and the right child how to deal with it? Since it is analog recursion, then it must be stored in the stack, who first into the stack? Considering the nature of the stack, we should let its right child first into the stack, left after the child into the stack, so that the top of the stack is left child, the next first out of the stack is the left child. This is consistent with the order of the first root traversal.
Look again at the left and right before the children sink into the stack. We got the top element of the stack. The element is still in the stack, so does it make sense in the stack? Obviously meaningless, because of its information we have output, and its left and right children in the stack will no longer need it, so it should be left and right children into the stack before the pop off.
def preorderiter (self, root): if None = = Root:return []re = []; s = []s.append (root) while Len (s): Cur_root = S.pop () print cur_root.valre.append (cur_root.val) if cur_root.right: S.append (Cur_root.right) if Cur_root.left:s.append (Cur_root.left) return re
Same. The same is true for post-root traversal. Although the post-root traversal is "left and right", we know where to put the root. The difference is the change in the stacking order of the child nodes.
def postorderiter (self, root): if None = = Root:returnre = [] # store RESULTSS = [] # node Stacks.append (root) while Len (s): C Ur_root = S.pop () re.insert (0,cur_root.val) if Cur_root.left:s.append (cur_root.left) if Cur_root.right:s.append (cur_ Root.right) return re
The trouble is that the middle root traverses "Zogen right". As mentioned earlier, the top node of the current stack we are dealing with is considered to be the root junction, but this root node does not know where to put in the results, put in front, the front should be left position. Put on the right, right side should be the child's position, put in the middle? Where is the middle? 1-10, 2 is the middle or 3 is the middle? We're not sure. We have no way of knowing the number of children left and right.
It seems that the top element at this point cannot be the same as the root and the back. Direct output, but also in the stack to squeeze a little better, or can not be directly discarded.
The current top root of the stack can not be output, because its left is not determined, then we just have to export its left. will be able to determine the location of the current root.
def inorderiter (self, root): if None = = Root:return []re = []s = []s.append] While Len (s): Cur_root = s[-1]# Push, unti L The last Letfwhile cur_root.left:s.append (cur_root.left) Cur_root = cur_root.left# pops, until one node has rightwhile len (s): Cur_root = S[-1]print cur_root.valre.append (cur_root.val) s.pop () if Cur_root.right:s.append (cur_root.right) Breakreturn RE
Non-recursive traversal of binary tree