Given a binary tree, return the preorder traversal of its nodes ' values.
For example:
Given binary Tree {1,#,2,3}
,
1 2 / 3
Return [1,2,3]
.
Note: Recursive solution is trivial, could do it iteratively?
This is titled Binary Tree First Order traversal, that is, root traversal, but the hint can not be recursive, so is the binary tree First order traversal of the non-recursive implementation. Now in order to be able to flexibly grasp the non-recursive traversal in the interview, three kinds of traversal methods are summed up, the aspects of the interview in the future review use.
First-order Traversal:
Recursion:
void Traverse (Tree *root) { if (root==null) return; Visit (root->data); Traverse (tree->left); Traverse (tree->right);}
Non-recursive (implemented by the stack, because the process of recursion is the program automatically press stack);
void Traverse (Tree *root) { stack.push (root); while (!stack.empty ()) { temp=stack.top (); Stack.pop (); Visit (temp->data); Stack.push (temp->right);//First put the right son into the stack, because later than the left son access to Stack.push (temp->left);} }
Middle Sequence Traversal:
Recursion:
void Traverse (Tree *root) { if (root==null) return; Traverse (tree->left); Visit (root->data); Traverse (tree->right); }
Post-post traversal:
Recursion:
void Traverse (Tree *root) { if (root==null) return; Traverse (tree->left); Traverse (tree->right); Visit (root->data); }
Non-recursive 1:The non-recursive implementation of the post-sequential traversal is the most difficult of three traversal methods. Because in the post-order traversal, to ensure that both the left child and the right child have been visited and the left child in front of the right child access to access to the root node, which brings the process of control problems. Here are two ideas. The first way of thinking: for any node p, put it into the stack, and then along its left subtree search down, until the search for a node without the left child, the node appears at the top of the stack, but at this time can not be stacked and access, so its right child is also visited. So next follow the same rules to the right subtree of the same processing, when the end of the access to their right child, the node appears on the top of the stack, it can be out of the stack and access. This guarantees the correct order of access. As you can see, in this process, each node appears at the top of the stack two times, and can only be accessed the second time it appears on top of the stack. Therefore, it is necessary to set more than one variable to identify whether the node appears first on the top of the stack.
Code implementation:
/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (NULL) , right (NULL) {}}; */class Solution {public: vector<int> preordertraversal (treenode* root) { treenode* temp; Stack<treenode *> St; St.push (root); while (!st.empty ()) { temp=st.top (); St.pop (); Visit (temp->val); St.push (temp->right); St.push (Temp->left);}} ;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode:binary Tree Preorder Traversal