Question:
Given a binary tree, returnPostorderTraversal of its nodes 'values.
For example:
Given Binary Tree{1,#,2,3}
,
1 2 / 3
Return[3,2,1]
.
Note:Recursive solution is trivial, cocould You Do It iteratively?
Note:
1) Two implementations: recursion and non-recursion. There are two methods for non-recursion.
2) Complexity Analysis: time O (N), Space O (N)
Implementation:
1. Recursion
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 public:12 vector<int> postorderTraversal(TreeNode *root) {13 vector<int> root_vec;14 vector<int> left_vec;15 vector<int> right_vec;16 if(root==NULL) return root_vec;17 if(root->left) left_vec=postorderTraversal(root->left);18 if(root->right) right_vec=postorderTraversal(root->right);19 root_vec.push_back(root->val);20 left_vec.insert(left_vec.end(),right_vec.begin(),right_vec.end());21 left_vec.insert(left_vec.end(),root_vec.begin(),root_vec.end());22 return left_vec;23 }24 };
View code
Ii. Non-recursion
In the descending order of traversal, the left subtree is accessed first, then the right subtree is accessed, and then the root node is accessed. For each subtree, The traversal is performed in the same order, the non-Recursive Implementation of post-order traversal is relatively difficult. To ensure that the root node can be accessed only after the left and right subtree are accessed, the idea is as follows:
For any node P,
1) first p the node into the stack;
2) If P does not have left or right children, or P has left or right children, but left and right children have been output, you can directly output node P and output it out of the stack, mark the output stack node P as the previous output node, and then set the top stack node as the current node;
3) if the conditions in 2) are not met, the right and left children of P are added to the stack in sequence. The current node is reset to the top node of the stack, and then the operation is repeated. 2 );
4) The traversal ends until the stack is empty.
A. the following code is more common and consistent with the above analysis.
1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10 class solution {11 public: 12 vector <int> postordertraversal (treenode * root) {13 vector <int> postorder_vec; 14 treenode * cur = root; // defines the pointer, point to the current node 15 treenode * pre = NULL; // define a pointer to the previous accessed node 16 if (cur = NULL) return postorder _ VEC; 17 stack <treenode *> postorder_stack; // create an empty stack 18 postorder_stack.push (cur); // first import the root node of the tree to stack 19 // until the stack is empty, end loop 20 while (! Postorder_stack.empty () 21 {22 cur = postorder_stack.top (); // set the current node to the top node of the stack 23 if (cur-> left = NULL & cur-> right = NULL) | 24 (pre! = NULL) & (cur-> left = pre | cur-> right = pre) 25 {26 // if the current node has no left or right children, or a left child or child, but has been accessed and output by 27 //, the node is output directly to the stack, set it to 28 postorder_stack.pop (); 29 postorder_vec.push_back (cur-> Val); 30 pre = cur; 31} 32 else33 {34 // if the preceding two conditions are not met, add the right child to the stack for 35 if (cur-> right! = NULL) postorder_stack.push (cur-> right); 36 IF (cur-> left! = NULL) postorder_stack.push (cur-> left); 37} 38} 39} 40 };
View code
B. the following code is concise (personal feeling, don't like it)
1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10 class solution {11 public: 12 vector <int> postordertraversal (treenode * root) 13 {14 vector <int> RS; 15 if (! Root) Return Rs; // if it is an empty tree, the system returns an empty vector16 stack <treenode *> STK; 17 STK. Push (Root); // the current node's 18 while (! STK. empty () 19 {20 treenode * t = STK. top (); // The stack top node outputs the stack and outputs 21 STK. pop (); 22 Rs. push_back (t-> Val); 23 // Note: The following stack import sequence cannot be incorrect, because first left and then right, 24 // in this way, the first traversal is right (center-> right-> left) 25 if (t-> left) STK. push (t-> left); 26 if (t-> right) STK. push (t-> right); 27} 28 reverse (RS. begin (), RS. end (); // in reverse order, 29 Return Rs; 30} 31} is traversed };
View code