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的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  4. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  6. leetcode 145. Binary Tree Postorder Traversal ----- java

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  8. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  9. leetcode - [6]Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  10. 【leetcode】Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

随机推荐

  1. 【数学 BSGS】bzoj2242: [SDOI2011]计算器

    数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...

  2. Linux 命令、配置文件及操作

    Linux 命令.配置文件及操作 命令 命令 参数 说明 A alias.unalias 命令别名 B C cat 查看文件内容 cd 切换目录 chown 修改拥有着 chgrp 修改所属组 chm ...

  3. Form和ModelForm组件

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  4. Python3爬虫一之(urllib库)

    urllib库是python3的内置HTTP请求库. ython2中urllib分为 urllib2.urllib两个库来发送请求,但是在python3中只有一个urllib库,方便了许多. urll ...

  5. scanf(),gets(),getchar()

    scanf()与gets()区别: scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets() ...

  6. Pond Cascade Gym - 101670B 贪心+数学

    题目:题目链接 思路:题目让求最下面池子满的时间和所有池子满的时间,首先我们考虑所有池子满的时间,我们从上到下考虑,因为某些池子满了之后溢出只能往下溢水,考虑当前池子如果注满时间最长,那么从第一个池子 ...

  7. POJ:1094-Sorting It All Out(拓扑排序经典题型)

    Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Description An ascending sorted sequence ...

  8. 全网最详细python中socket套接字send与sendall的区别

    将数据发送到套接字. 套接字必须连接到远程套接字.  返回发送的字节数. 应用程序负责检查是否已发送所有数据; 如果仅传输了一些数据, 则应用程序需要尝试传递剩余数据.(需要用户自己完成) 将数据发送 ...

  9. Selenium WebDriver的多浏览器测试

    1. IE浏览器,需要配合下载IEDriverSever.exe的驱动程序,目前selenium支持IE9以上. (驱动程序下载链接:https://pan.baidu.com/s/1YpaUsIs1 ...

  10. Mysql - 安装及初始化设置

    1. 下载mysql-5.7.13-tar.gz http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x8 ...