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 ...
随机推荐
- 【思维题 集合hash 树上差分】11.5撸树
要注重问题的转化和一些结论的推断 题目描述 要致富,先撸树. 一棵树的形状可以简化为一张 $N$ 个点 $M$ 条边的图,由于装备条件限制,你只有撸两次,也就是删去两条边,当这张图不联通时,就意味着树 ...
- Unity基础-发布
PC BuildSetting File->BuildSetting Switch Platform Development Build是在调试模式下使用版本 Script Debugging调 ...
- linux文件属性文文件类型知识
文件类型分别介绍: 1.普通文件:我们通过用ls -l来查看xxx.sql的属性,可以看到第一列内容为-rw-r--r--,值得注意的是第一个符号是-(英文字符减号),在Linux中,以这样的字符开 ...
- 《零基础入门学习Python》【第一版】视频课后答案第002讲
测试题答案: 0. 什么是BIF?BIF 就是 Built-in Functions,内置函数.为了方便程序员快速编写脚本程序(脚本就是要编程速度快快快!!!),Python 提供了非常丰富的内置函数 ...
- leetcode-4-basic
解题思路:这道题比较简单,代码不贴了.需要注意的是: 数字与字符串之间的转换, char str[100]; sprintf(str, "%d", num); 解题思路: 这道题是 ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- poj 1742 多重背包问题 dp算法
题意:硬币分别有 A1.....An种,每种各有C1......Cn个,问组成小于m的有多少种 思路:多重背包问题 dp[i][j]表示用前i种硬币组成j最多剩下多少个 dp=-1的表示凑不齐 dp ...
- 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js
估计网上能查到的最多的两种图片延迟加载方法就是jquery.scrollLoading.js与jquery.lazyload.js了,其中jquery.lazyload.js的调用方法因为有网友爆出的 ...
- maven无法下载依赖jar包—几种仓库的区别
一.问题背景 最近这两天,感觉自己智商急剧退化,到了自己都捉急的地步,呃,有必要记录下来,以后智商被人甩几条街的时候,看看这篇文字,找找灵感也是好的! 这个项目呢,是用IDEA开发的,我一切都弄好了, ...
- js多少时间之前
<?php $time = time()*1000; $end_time = strtotime("2018-01-01")*1000; $time_ago = $time ...