First, pre-sequence traversal
Analysis:
1, the pre-sequence traversal is around, so the use of stacks, record the top root node of the stack at the same time, the first pressure on the right sub-tree, back pressure left sub-tree, thus first traversing the left subtree.
2, the top node of each pop-up is in accordance with the sequence of the pre-order traversal, so it can be recorded directly.
3, note: Because the stack is pressed into the tree node, so the data type in the stack is node*.
struct node{ int val; Node *left, *right;}; Vector<int> Get_preorder (Node *root) { vector<int> preorder; Stack<node*> St; St.push (root); while (!st.empty ()) { node* top = st.top (); St.pop (); Preorder.push_back (Top-to-Val); St.push (top-right); St.push (top-to-left); } return preorder;}
Second, middle sequence traversal
Analysis:
1, the middle sequence traversal is left and right, for a tree, using the stack, you should first press into the root node of the right subtree, and then press into the root node value, and then press into the root node of the left subtree.
2, so operation, when the stack top element is a number of time to record, so write a structure stack_element to determine the current pressure on the stack is the node or node value.
struct node{ int val; Node *left, *right;}; struct stack_element{ Node *node; int Val; BOOL Isnode; Stack_element (node *_node, int _val, bool _isnode): Node (_node), Val (_val), Isnode (_isnode) {}};vector<int> get_ Inorder (Node *root) { vector<int> inorder; Stack<stack_element> St; St.push (Stack_element (Root,-1, true));//Since the element in the stack is a node, it is not important how much his value is, so this is set to 1 while (!st.empty ()) { Stack_ Element top = St.top (); St.pop (); if (top.isnode) { if (Top.node = = NULL) continue; St.push (Stack_element (Top.node-Right,-1, true)); St.push (Stack_element (NULL, Top.node, Val, false)); St.push (Stack_element (Top.node-left,-1, true)); } else{ Inorder.push_back (top.val); } } return inorder;}
Third, post-sequential traversal
Analysis:
1. The element that is considered to be pressed into the stack is the value of the node or node, as in the middle sequence traversal.
2, the only difference is the traversal order, so the middle sequence traversal of the code (20~22 line) slightly changed.
struct node{ int val; Node *left, *right;}; struct stack_element{ Node *node; int Val; BOOL Isnode; Stack_element (node *_node, int _val, bool _isnode): Node (_node), Val (_val), Isnode (_isnode) {}};vector<int> get_ Postorder (Node *root) { vector<int> postorder; Stack<stack_element> St; St.push (Stack_element (Root,-1, true));//Since the element in the stack is a node, it is not important how much his value is, so this is set to 1 while (!st.empty ()) { Stack_ Element top = St.top (); St.pop (); if (top.isnode) { if (Top.node = = NULL) continue; St.push (Stack_element (NULL, Top.node, Val, false)); St.push (Stack_element (Top.node-Right,-1, true)); St.push (Stack_element (Top.node-left,-1, true)); } else{ Postorder.push_back (top.val); } } return postorder;}
Pre-order, middle order and sequential traversal of binary tree with non-recursive recursion