Leetcode 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?
vector<int> postorderTraversal(TreeNode *root){
vector<int> res;
if(root == NULL) return res;
stack<TreeNode *> record;
record.push(root);
TreeNode *pre = NULL;
while(!record.empty()){
TreeNode *node = record.top();
if((node->left == NULL && node->right == NULL)||(pre!=NULL &&(pre == node->left || pre == node->right)) ){
res.push_back(node->val);
record.pop();
pre = node;
}else{
if(node->right) record.push(node->right);
if(node->left ) record.push(node->left);
}
}
return res;
}
另一种方法:
通过不断的交换左右孩子,然后压栈,最后弹出,即可得到结果
时间复杂度为O(h),h为树的高度,空间复杂度为O(n)
vector<int> postorderTraversa(TreeNode *root){
vector<int> res;
if(root == NULL) return res;
stack<TreeNode *> post_record,reverse_record;
post_record.push(root);
while(!post_record.empty()){
TreeNode *node = post_record.top();
reverse_record.push(node);
post_record.pop();
if(node->left) post_record.push(node->left);
if(node->right) post_record.push(node->right);
}
while(!reverse_record.empty()){
res.push_back(reverse_record.top()->val);
reverse_record.pop();
}
return res;
}
Leetcode Binary Tree Postorder Traversal的更多相关文章
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- LeetCode Binary Tree Postorder Traversal(数据结构)
题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- Clr Via C#读书笔记---CLR寄宿和应用程序域
#1 CLR寄宿: 开发CLR时,Microsoft实际是将他实现成包含在一个dll中的COM服务器.Microsoft为CLR定义了一个标准的COM接口,并为该接口和COM服务器分配了GUID.安装 ...
- SVM 最大间隔目标优化函数(NG课件2)
目标是优化几何边距, 通过函数边距来表示需要限制||w|| = 1 还是优化几何边距,St去掉||w||=1限制转为普通函数边距 更进一步的,可以固定函数边距为1,调节||w| ...
- Myeclipse的web工程和Eclipse互相转换
eclipse的web工程转myeclipse的web工程1.原eclipse工程叫netschool 2.在myeclipse中新建一个工程叫netschool 并在新建的时修改 web root ...
- blender源代码编译
blender源码路径(svn):https://svn.blender.org/svnroot/bf-blender/trunk/blender/ 依赖外部Lib(svn):https://svn. ...
- Linux进程状态 ( Linux Process State Codes)
进程状态代码及说明: STATE代码 说明 D 不可中断的睡眠. 通常是处于I/O之中. R 运行中/可运行. 正处于运行队列中. S 可中断的睡眠. 等待某事件发生. T 已停止. 可能是因为she ...
- MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法
在MySQL应用时,经常要使用这两个函数TIMESTAMPDIFF和TIMESTAMPADD. 一,TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval,datetime_ ...
- scala的tcp通信
client: object ActorClient extends App { import actors.Actor, actors.remote.Node, actors.remote.Remo ...
- elasticsearch入门
到 https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.6.0.zip 下载最新包: 启动: ./elast ...
- html5 svg 圆形进度条
html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- iphone手机不同版本兼容、横竖屏
/* 兼容问题*/ @media screen and (device-width: 320px) and (device-height: 480px) and (-webkit-device-pix ...