翻译

给定一个二叉树。返回其兴许遍历的节点的值。

比如:
给定二叉树为 {1。 #, 2, 3}
1
\
2
/
3
返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗?

原文

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) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
v.push_back(root->val);
}
return v;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> v; void postorderTraversalIter(TreeNode *root, stack<TreeNode*> &stac) {
if (root == NULL) return;
bool hasLeft = root->left != NULL;
bool hasRight = root->right != NULL;
stac.push(root);
if (hasRight)
stac.push(root->right);
if (hasLeft)
stac.push(root->left);
if (!hasLeft && !hasRight)
v.push_back(root->val);
if (hasLeft) {
root = stac.top();
stac.pop();
postorderTraversalIter(root, stac);
}
if (hasRight) {
root = stac.top();
stac.pop();
postorderTraversalIter(root, stac);
}
if (hasLeft || hasRight)
v.push_back(stac.top()->val);
stac.pop();
} vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> stac;
postorderTraversalIter(root, stac);
return v;
}
};

另有两道相似的题目:

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Java for LeetCode 145 Binary Tree Postorder Traversal

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

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

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

  8. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  9. Leetcode#145 Binary Tree Postorder Traversal

    原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...

随机推荐

  1. [Python爬虫] 之十三:Selenium +phantomjs抓取活动树会议活动数据

    抓取活动树网站中会议活动数据(http://www.huodongshu.com/html/index.html) 具体的思路是[Python爬虫] 之十一中抓取活动行网站的类似,都是用多线程来抓取, ...

  2. Windows10的周年更新中无法关闭Cortana?这里有方法

    备受期待的Windows 10的周年更新中将会带来诸多新特性,包括更实用的开始菜单.Windows Ink.强化的Windows Defender,甚至还有一个无法关闭的语音助手Cortana. 目前 ...

  3. Linux远程执行Windows机器任务

    Linux远程执行Windows机器任务     近期测试人员提出需求需要在Linux下调用Windows系统下的cmd的命令完成自动构建和测试并生成测试报告. 环境: Windows Server2 ...

  4. 一个tomcat中部署多个项目

    在各自的项目web.xml中添加 <context-param> <param-name>webAppRootKey</param-name> <param- ...

  5. next 前缀字符串

    我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我们可以看到这个算法针对的是子串有对称属性, ...

  6. zabbix_LAMP源码安装

    Zabbix源码包安装 Cenos5.3 Basic server 安装顺序 Libxml2 Libmcrypt Zlib Libpng Jpeg:需要创建目录jpeg /bin /lib /incl ...

  7. eclipse中使用mybatis-generator逆向代码生成工具问题解决记录

    问题一: eclipse中使用mybatis-generator逆向代码生成工具出现waiting for "building  workspace" 解决办法: 选择菜单栏的   ...

  8. 开机自启动:从busybox到debian

    需要在mint上设置opensips的开机自启动,翻了半天资料还是一知半解.最后在opensips的官方文档,查到用下面的语句,添加自启动成功.不过貌似还是会有启动不成功,没有仔细测试过. updat ...

  9. hdu 3572 Task Schedule(最大流&amp;&amp;建图经典&amp;&amp;dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. linux下的QQ执行玩法:pidgin-lwqq

    安装pidgin: sudo apt-get install pidgin 安装pidgin-lwqq: sudo add-apt-repository ppa:lainme/pidgin-lwqq ...