145. Binary Tree postorder Traversal non-recursive, stack implementation

Source: Internet
Author: User

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] .

/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (NULL) , right (NULL) {}}; *//*1) If the root node is not empty, add the root node to the stack.   2) If the stack is not empty, take the top element of the stack (temporarily does not eject), if (the left subtree has been accessed or the left subtree is empty), and (the right subtree has been accessed or the right subtree is empty), the top node of the stack pops up, adds its value to the array, and if the left dial hand tree is not empty and has not been accessed, the left child node is added to the stack and the left subtree has been visited. If the right subtree is not empty and has not been accessed, the right child node is added to the stack and the right subtree has been visited. 3) Repeat the second step until the stack is empty.      */struct stknode{TreeNode *node;      BOOL lvisited;      BOOL rvisited; Stknode (TreeNode *p) {node = p; lvisited= false; rvisited= false;}};        Class Solution {public:vector<int> Postordertraversal (treenode* root) {stack<stknode*> Stk;        vector<int> result;        if (root==null) return result;        Stk.push (new Stknode (root));            while (!stk.empty ()) {Stknode *stknode = Stk.top (); if ((!stknode->node->left | | stknode->lvisited) && (!stknode->node->right | | stknode->      rvisited)) {Stk.pop ();         Result.push_back (Stknode->node->val);            Delete Stknode; }else if (stknode->node->left &&!stknode->lvisited) {Stk.push (new Stknode (Stkno               De->node->left));            Stknode->lvisited = true; }else if (stknode->node->right &&!stknode->rvisited) {Stk.push (new Stknode (Stk               Node->node->right));             Stknode->rvisited = true;    }} return result; }};

145. Binary Tree postorder Traversal non-recursive, stack implementation

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.