LeetCode OJ--Binary Tree Postorder Traversal
http://oj.leetcode.com/problems/binary-tree-postorder-traversal/
树的后序遍历,可以使用递归,也可以使用栈,下面是栈的实现代码
#include <iostream>
#include <vector>
#include <stack>
using namespace std; 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> ans;
if(root == NULL)
return ans;
stack<TreeNode*> myStack;
myStack.push(root);
TreeNode* lastPop = NULL;
TreeNode* nodeTop = NULL;
while(!myStack.empty())
{
nodeTop = myStack.top();
if(nodeTop->right && lastPop != nodeTop->right)
myStack.push(nodeTop->right);
else if(nodeTop->right && lastPop == nodeTop->right)
{
ans.push_back(nodeTop->val);
lastPop = nodeTop;
myStack.pop();
continue;
}
if(nodeTop->left && (nodeTop->right &&lastPop != nodeTop->right || nodeTop->right == NULL && lastPop != nodeTop->left))
myStack.push(nodeTop->left);
else if(nodeTop->left && (nodeTop->right && lastPop == nodeTop->right || nodeTop->right == NULL && lastPop == nodeTop->left))
{
ans.push_back(nodeTop->val);
lastPop = nodeTop;
myStack.pop();
continue;
}
if(nodeTop->left == NULL && nodeTop->right == NULL)
{
ans.push_back(nodeTop->val);
lastPop = nodeTop;
myStack.pop();
}
}
return ans;
}
}; int main()
{
TreeNode *root = new TreeNode();
TreeNode *n2 = new TreeNode();
//TreeNode *n3 = new TreeNode(2);
TreeNode *n4 = new TreeNode();
//TreeNode *n5 = new TreeNode(5);*/
TreeNode *n6 = new TreeNode();
TreeNode *n7 = new TreeNode(); root->left = n2;
//root->right = n3;
n2->left = n4;
//n2->right = n5;*/
//n3->left = n6;
//n6->right = n7;
Solution myS; myS.postorderTraversal(root);
return ;
}
LeetCode OJ--Binary Tree Postorder Traversal的更多相关文章
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
		
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
 - C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
		
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
 - LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
		
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
 - [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
		
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
 - Java for LeetCode 145 Binary Tree Postorder Traversal
		
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
 - leetcode 145. Binary Tree Postorder Traversal ----- java
		
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 bina ...
 - LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
		
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
 - leetcode - [6]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
		
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
 
随机推荐
- 传智 Python基础班+就业班+课件 【最新完整无加密视频课程】
			
点击了解更多Python课程>>> 传智 Python基础班+就业班+课件 [最新完整无加密视频课程] 直接课程目录 python基础 linux操作系统基础) 1-Linux以及命 ...
 - win10安装pytorch——前面有坑,快跳进去鸭
			
嗯!花费了不少时间才把pytorch安装成功.主要原因就是: 清华和中科大的Anaconda国内镜像源关闭了 activate.bat 不是内部或外部命令(这个真实奇怪) 1. 安装过程 可以去Ana ...
 - Mysql源码编译安装&主从复制
			
一)camke源码编译安装mysql 1)创建软件安装目录software [root@master software]# ls cmake-2.8.8.tar.gz mysql-5.5.32.tar ...
 - 计算n的阶乘(n!)末尾0的个数
			
题目: 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数. 举例: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 24 ...
 - day01_06.比较运算符
			
> >= = < <= != == === !== 凡运算,必有运算结果,比较运算符的运算结果是布尔值 ==和===的区别 <?php $c = ( 3 == ...
 - c中#与##的应用思考
			
c中#与##的应用思考 原创 2014年02月25日 22:01:35 927 一. 思考出处 在读<<linux 0.12完全剖析>>初始化部分, init进程是通过fork ...
 - TensorFlow——小练习:feed
			
feed就是喂入数据 使用feed前必须要有占位符作为操作的对象,在运行操作的时候喂入数据. # _*_coding:utf-8_*_ import tensorflow as tf import n ...
 - python闭包函数、装饰器
			
闭包函数的传值方式: 方式1:通过参数传值 def func(x): print(x)func(1) 方式2:闭包函数传值 def outter(x): def inner(): print(x) r ...
 - 面向对象编程(二)封装--构造方法,this关键字,static关键字,方法重载
			
面向对象三大特点:封装.继承.多态 封装概念 ① 将东西包装在一起,然后以新的完整形式呈现出来: 将方法和字段一起包装到一个单元中,单元以类的形式实现; ② 信息隐藏,隐藏对象的实现细节,不让 ...
 - springMVC 引入静态资源Js的方式
			
前两天项目出现了Js无法引入的情况,本篇博客先总结分析+批判自己犯的低级错,再说说几种访问静态资源的方式! 首先,由于在web.xml里面的servlet拦截匹配为<url-pattern> ...