145. Binary Tree Postorder Traversal

原创 2016年05月30日 10:05:55

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 you do it iteratively?

题意:迭代进行后续遍历。

思路:使用栈。

/**
 * 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> postorderTraversal(TreeNode* root) {
		struct TreeNodeFlag{
			TreeNode* node;
			bool flag;
			TreeNodeFlag(TreeNode* x) :node(x), flag(false){}
		};
		vector<int> res;
		stack<TreeNodeFlag *> mystack;
		TreeNodeFlag* p = new TreeNodeFlag(root);
		if (p->node){
			mystack.push(p);
			while (p->node->left){
				p = new TreeNodeFlag(p->node->left);
				mystack.push(p);
			}
			while (p->node->right){
				p->flag = true;
				p = new TreeNodeFlag(p->node->right);
				mystack.push(p);
				while (p->node->left){
					p = new TreeNodeFlag(p->node->left);
					mystack.push(p);
				}
			}
		}
		
		while (!mystack.empty()){
			TreeNodeFlag* t = mystack.top();
			if (t->flag == true || t->node->right == NULL){
				res.push_back(t->node->val);
				mystack.pop();
			}
			else{
				while (t->node->right){
					t->flag = true;
					t = new TreeNodeFlag(t->node->right);
					mystack.push(t);
					while (t->node->left){
						t = new TreeNodeFlag(t->node->left);
						mystack.push(t);
					}
				}
			}
		}
		return res;
	}
};








版权声明:本文为博主原创文章,未经博主允许不得转载。

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016年05月25日 00:48
  • 5206

LeetCode(145)Binary Tree Postorder Traversal

题目如下: Given a binary tree, return the postorder traversal of its nodes' values. 1 \ 2 ...
  • feliciafay
  • feliciafay
  • 2014年01月16日 04:01
  • 3574

LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: ...
  • u012249528
  • u012249528
  • 2015年07月09日 10:34
  • 904

Binary Tree Postorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/  跟Binary Tree Inorder Traver...
  • linhuanmars
  • linhuanmars
  • 2014年03月25日 01:10
  • 12913

106 Construct Binary Tree from Inorder and Postorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/题目:Giv...
  • ChilseaSai
  • ChilseaSai
  • 2015年12月09日 20:29
  • 877

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-J...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月09日 06:39
  • 2212

[LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You m...
  • Evan123mg
  • Evan123mg
  • 2015年07月20日 21:23
  • 648

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢...
  • kenden23
  • kenden23
  • 2013年11月12日 13:20
  • 2290

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

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin...
  • yyywww666
  • yyywww666
  • 2017年04月28日 11:29
  • 522

LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal

题目链接:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trave...
  • makuiyu
  • makuiyu
  • 2015年04月17日 16:44
  • 556
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:145. Binary Tree Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)