Topic:
Given a binary tree, return the postorder traversal of its nodes ' values.
For example:
Given binary Tree {1,#,2,3},
1 2 / 3
return [3,2,1].
Note:recursive solution is trivial, could do it iteratively?
Recursive solution (c + + version):
/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (NULL), right (N ULL) {} *}; */ class solution{private : vector <int > result; public : vector <int ; postordertraversal (TreeNode *root) {if (Root) {postordertraversal (root->left); Postordertraversal (Root->right); Result.push_back (Root->val); } return result; }};
Two cross-tree non-recursive post-traversal idea:
Binary tree non-recursive follow-up traversal is the most difficult of three kinds of traversal, because the subsequent traversal finally accesses the root node, the operation does not go back to the root node, while the pre-sequence traversal and the middle sequence traversal, each loop traversal is from the root node to start backtracking is easy.
A lot of code on the Internet is using a stack stack to operate, the code is not easy to understand, read very hard.
In fact, you can use two stacks to perform a non-recursive follow-up traversal of a binary tree. First access the root node, into the stack (a stack), remember the root node, the stack into another stack (b stack), and then the root node left and right subtree into the stack (a stack). The result of the final output B stack is ok.
/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classsolution{ Public: vector<int>Postordertraversal (TreeNode *root) { vector<int>Resultif(!root)returnResult Stack<TreeNode*>Outputstack; Stack<TreeNode*>Middlestack; Middlestack.push (root); TreeNode *node =nullptr; while(!middlestack.empty ()) {node = Middlestack.top (); Middlestack.pop (); Outputstack.push (node);if(Node->left) {Middlestack.push (node->left); }if(Node->right) {Middlestack.push (node->right); } } while(!outputstack.empty ()) {Result.push_back (Outputstack.top ()->val); Outputstack.pop (); }returnResult }};
C # Non-recursive reference code:
/** * Definition for Binary tree * public class TreeNode {* public int val; * Public TreeNode left; * Pub Lic TreeNode right; * Public TreeNode (int x) {val = x;} *} */ Public class solution{ Publicilist<int>Postordertraversal(TreeNode Root) {ilist<int> result =Newlist<int> ();if(Root = =NULL)returnResult Stack<treenode> Outputstack =NewStack<treenode> (); Stack<treenode> Middlestack =NewStack<treenode> (); Middlestack.push (root); TreeNode node =NULL; while(Middlestack.count! =0) {node = Middlestack.pop (); Outputstack.push (node);if(Node.left! =NULL) Middlestack.push (Node.left);if(Node.right! =NULL) Middlestack.push (node.right); } while(Outputstack.count! =0) {result. ADD (Outputstack.pop (). val); }returnResult }}
Leetcode:binary Tree postorder Traversal (binary tree post-traversal)