[LeetCode] Binary Tree Postorder Traversal, leetcodepostorder

Source: Internet
Author: User

[LeetCode] Binary Tree Postorder Traversal, leetcodepostorder

Given a binary tree, return the postorder traversal of its nodes 'values.

For example:
Given binary tree {1, #, 2, 3 },

Return [3, 2, 1].

Note: Recursive solution is trivial, cocould you do it iteratively?

Recursive Implementation Code
/*************************************** * ************************** @ Author: chuxing * @ Date: 2015/2/24 * @ Status: Accepted * @ Runtime: 4 ms ************************************** * **************************/struct TreeNode {int val; treeNode * left; TreeNode * right; TreeNode (int x): val (x), left (NULL), right (NULL) {}}; class Solution {public: void helper (TreeNode * root, vector <int> & result) {if (root) {helper (root-> left, result); helper (root-> right, result ); result. push_back (root-> val) ;}} vector <int> postorderTraversal (TreeNode * root) {vector <int> result; helper (root, result); return result ;}};
Non-Recursive Implementation Code 1
Class Solution {public: vector <int> postorderTraversal (TreeNode * root) {vector <int> result; if (root = NULL) {return result ;} list <TreeNode *> nodes; TreeNode * p = root; TreeNode * cur; nodes. push_back (p); while (! Nodes. empty () {cur = nodes. front (); // ① the current node is not a leaf node, but the child has already traversed it. // ② The current node is the leaf node if (cur-> right = p | cur-> left = p | (cur-> left = NULL & cur-> right = NULL )) {result. push_back (cur-> val); nodes. pop_front (); p = cur;} else {if (cur-> right! = NULL) {nodes. push_front (cur-> right);} if (cur-> left! = NULL) {nodes. push_front (cur-> left) ;}}return result ;}};
Non-Recursive Implementation Code 2
class Solution {public:    vector<int> postorderTraversal(TreeNode *root) {        vector<int> result;        stack<TreeNode*> nodes;        TreeNode *p = NULL;        TreeNode *cur = root;        while(cur != NULL || !nodes.empty())        {            if (cur != NULL)            {                while (cur != NULL)                {                    nodes.push(cur);                    cur = cur->left;                }            }            else            {                cur = nodes.top();                if (cur->right == NULL || cur->right == p)                {                    nodes.pop();                    p = cur;                    result.push_back(cur->val);                    cur = NULL;                }                else                {                    cur = cur->right;                }            }        }        return result;    }};

For the implementation of first-order Traversal, see the blog post Binary Tree Preorder Traversal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.