LeetCode Binary Tree PostorderTranversal
Problem Description
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},1
\
2
/
3return
[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
Problem Solution
1. 递归方案
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int> nodeVec;
public:
void traverse(TreeNode *root){
if(root==NULL)
return;
traverse(root->left);
traverse(root->right);
nodeVec.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode *root) {
traverse(root);
return nodeVec;
}
};
2. 非递归方案
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int> nodeVec;
public:void iterTraverse(TreeNode *root){
if(root==NULL)
return;
stack<TreeNode*> st; TreeNode *pCur,*pPrev=NULL; //pCur: current tree node, pPrev: previous visited tree node
st.push(root);
while(!st.empty())
{
pCur=st.top();
if((pCur->left == NULL && pCur->right == NULL) || (pPrev != NULL && (pCur->left==pPrev || pCur->right==pPrev)))
{
nodeVec.push_back(pCur->val);
pPrev=pCur;
st.pop();
}
else
{
if(pCur->right != NULL)
st.push(pCur->right);
if(pCur->left != NULL)
st.push(pCur->left);
}
}
}
vector<int> postorderTraversal(TreeNode *root) {
iterTraverse(root);
return nodeVec;
}
};
LeetCode Binary Tree PostorderTranversal的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- 剑桥offer(41~50)
41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...
- scanf函数用法小记
By francis_hao Aug 26,2017 scanf – 输入格式转换 概述 #include <stdio.h>int scanf(const char *fo ...
- MyEclipse下项目的包层次结构调整
新电脑安装完MyEclipse,导入项目后发现MyEclipse下项目的包层次结构变成了Flat,平面模式,这种模式感觉特别不好, 不能清晰地显示出项目的包层次结构.这样,显示出的包的结构不够明显,我 ...
- hzwer分块九题(暂时持续更新)
hzwer分块9题 分块1:区间加法,单点查询 Code #include<bits/stdc++.h> #define in(i) (i=read()) using namespace ...
- 解决 cmd dos 下 无法显示中文
在做程序开发的时候经常需要在使用命令行进行操作, dos环境本身是不支持中文的,有时候中文编码的问题就像苍蝇一样讨厌,下面提供几种常用的手段解决win7环境下中文显示乱码的问题: 方法一: 修改注册表 ...
- springboot的application.properties与.yml的区别
现在我们的application.properties文件内容是: server.port=8090 server.session-timeout=30 server.context-path= se ...
- [zabbix]zabbix分区表操作步骤
Q&A: 1.mul key: . 如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列 . 如果Key是PRI, 那么该列是主键的组成部分 . ...
- stout代码分析之七:Result类
Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...
- main函数的传参与返回
1.谁给main函数传参(1)调用main函数所在的程序的它的父进程给main函数传参,并且接收main的返回值.2.为什么需要给main函数传参(1)首先,main函数不传参是可以的,也就是说父进程 ...
- ubuntu中使用virtualbox遇到Kernel driver not installed (rc=-1908)错误
百度之后得到解决,再此做个笔记 错误提示 Kernel driver not installed (rc=-1908) The VirtualBox Linux kernel driver (vbox ...