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 ...
随机推荐
- EF – 7.一对多关联
5.6.8 <一对多关联(上)> 5.6.9 <一对多关联(下)> 一对多的关联,可以说是整个数据库应用程序中最常见的一种关联类型了,因此,必须高度重视这种关联类型CRUD的实 ...
- MySQL基础二
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...
- 【openGL】画圆
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- Linux下修改默认字符集--->解决Linux下Java程序种中文文件夹file.isDirectory()判断失败的问题
一.问题描述: 一个项目中为了生成树状目录,调用了file.listFiles()方法,然后利用file.isDirectory()方法判断是否为目录,该程序在windows下运行无问题,在Linux ...
- 团队作业-第一周-NABCD竞争性需求分析
1. Need 需求 随着科技信息的发展,传统的课堂点名亟待步入信息处理的轨道,移动校园课堂点名软件恰好的切入了这个需求点,市场中词类软件也为数不多,因此需求也是比较强烈. 2. Approac ...
- css 全局 兼容性问题
css 笔记 第一种:常用的全局CSS属性设置 //参考大型网站,如凤凰网 (1)清除所有的标记的内外边距 body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,p ...
- android 入门-Service
sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...
- vim用法小节
1.把一个文件的内容全选复制到另一个文件 方案一: gg"*yG 然后另外一个vim "*p "*是系统剪贴板寄存器 方案二: 打开另一个文件,然后输入 :r filen ...
- linux 用户之间的切换
从root用户切换到普通用户fxm, 使用如下命令:su - fxm 从普通用户切换到root用户,使用如下命令:su - 或者 su, root可以省略不写.
- java发展史与java的语言特性
概述: Java 体系比较庞杂,功能繁多,这也导致很多人在自学 Java 的时候总是感觉无法建立 全面的知识体系, 无法从整体上把握Java 的原因. 在这里我们先简单了解一下Java 的版本. 具体 ...