Binary Tree Postorder Traversal——重要的基本的算法
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?
这道题想了很久,后序遍历应该是最麻烦的吧,最容易想到的方法就是直接设置标志位。
相比于前序遍历,后续遍历思维上难度要大些,前序遍历是通过一个stack,首先压入父亲结点,然后弹出父亲结点,并输出它的value,之后压人其右儿子,左儿子即可。然而后序遍历结点的访问顺序是:左儿子 -> 右儿子 -> 自己。那么一个结点需要两种情况下才能够输出:第一,它已经是叶子结点;第二,它不是叶子结点,但是它的儿子已经输出过。那么基于此我们只需要记录一下当前输出的结点即可。对于一个新的结点,如果它不是叶子结点,儿子也没有访问,那么就需要将它的右儿子,左儿子压入。如果它满足输出条件,则输出它,并记录下当前输出结点。输出在stack为空时结束。
网上看到一种简洁的方法,如果一个节点的左子树或右子树入栈后,就将其相应的左子树或右子树设置为NULL,思想简单,代码也简单,代码如下:
/**
* 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) {
vector<int> res;
stack<TreeNode*> stk;
TreeNode* flag=root;
if(root==NULL)
return res;
stk.push(root);
while(!stk.empty())
{
flag=stk.top();
if(flag->left!=NULL)
{
stk.push(flag->left);
flag->left=NULL;
}
else if(flag->right!=NULL)
{
stk.push(flag->right);
flag->right=NULL;
}
else
{
res.push_back(flag->val);
stk.pop(); }
}
return res;
}
};
设立标志位的解法如下:
/**
* Definition for binary tree
* 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) {
vector<int> result;
stack<TreeNode*> node;
stack<int> nodeStatus; if(root != NULL){
node.push(root);
nodeStatus.push();
} while(!node.empty()){
TreeNode* n = node.top();
node.pop();
int status = nodeStatus.top();
nodeStatus.pop(); if(status == ){
node.push(n);
nodeStatus.push();
if(n->right != NULL){
node.push(n->right);
nodeStatus.push();
}
if(n->left != NULL){
node.push(n->left);
nodeStatus.push();
}
}
else{
result.push_back(n->val);
}
} return result;
}
Binary Tree Postorder Traversal——重要的基本的算法的更多相关文章
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
随机推荐
- 删除空格-sed
如下,我需要提取出‘wan’这个字符串.可以发现在‘wan’的前后是有空格,需要将其删除. # lxc list # lxc list | grep lxdbr0 | awk -F "|&q ...
- The Shortest Path in Nya Graph HDU - 4725
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hibernate、mybatis、beetsql的学习
先推荐两篇文章吧: https://my.oschina.net/xiandafu/blog/617542 http://blog.csdn.net/xiandafu/article/details/ ...
- bzoj 1068 [SCOI2007]压缩 区间dp
[SCOI2007]压缩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1644 Solved: 1042[Submit][Status][Discu ...
- Java中的String为什么是不可变的? — String源码分析
原文地址:http://www.importnew.com/16817.html 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为 ...
- linux查看文件相关指令
以下内容整理自以下两篇文章: http://www.cnblogs.com/xilifeng/archive/2012/10/13/2722596.html Linux 查看文件内容的命令 http: ...
- 关于$.data(element,key,value)与ele.data.(key,value)的区别
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- UVA 1262 Password
https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...
- 转【es中数据节点和主机】
在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题. 默认情况下,elasticsearch集群中每个节点都有成为主节点的资格,也都存储 ...
- 【51NOD-0】1130 N的阶乘的长度 V2(斯特林近似)
[算法]数学 [题解]斯特林公式: #include<cstdio> #include<algorithm> #include<cmath> using names ...