leetcode - [6]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]
.
思路:后序遍历是按照“左子树,右子树,根”的顺序访问元素。那么根或者其它父亲元素就要先压入栈,然后再弹出。
#include <iostream>
#include <algorithm>
#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> res;
stack<TreeNode *> s;
if (!root) {
return res;
}
s.push(root);
while (!s.empty()) {
TreeNode *p = s.top(); s.pop();
res.push_back(p->val); if (p->right) {
s.push(p->right);
} if (p->left) {
s.push(p->left);
}
}
reverse(res.begin(), res.end());
return res;
}
}; int main(int argc, char *argv[]) {
TreeNode *p = new TreeNode();
p->right = new TreeNode();
p->left = new TreeNode(); Solution *solution = new Solution(); vector<int> res;
res = solution->postorderTraversal(p); vector<int>::iterator it;
for (it = res.begin(); it != res.end(); it++) {
cout << *it << endl;
} }
leetcode - [6]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】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
随机推荐
- Charles抓取https包
Android: 1.若滑动解锁,请先设置屏幕锁: 2.然后浏览器输入Charlesproxy.com/getssl,安装证书,出现安装证书提示,随便打个名称 比如Android,选择WLAN(这里A ...
- Windows Live Writer
一.简介 Windows Live Writer 是一个强大的离线博客编辑工具,通过它可以离线编辑内容丰富的博文,除了自身强大的编辑功能之外,还提供了接口,让其它开发人员通过插件提供工具自身没有提供的 ...
- RDMA的基础概念
一张图可以简单明确的说明,目前RDMA的几种技术的差别: RDMA是remote Direct memory access的简称,有几个最基本的特点: CPU offload kernel bypas ...
- MCS-51与8086指令系统比较
- 关于C的int
在c运行库头文件<stdint.h>中typedef各种类型的int typedef signed char int8_t; typedef unsigned char uint8_t; ...
- c++文件中引用C代码
下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...
- go语言字符串练习
package main import "fmt" import s"strings" var p = fmt.Println func main() { p( ...
- mysql mariadb的VC客户端遇到的问题
在使用VS2017编写数据库客户端 具体设置可参见以下内容 https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows- ...
- Mysql遇到的坑
2018-04-09 这个虽然跟粗心有关,但是Mysql没报错是哪般? select sum(play_count) from tb_user_login where user_id = 61 and ...
- DHT
DHT(Distributed Hash Table,分布式哈希表)类似Tracker的根据种子特征码返回种子信息的网络.DHT全称叫分布式哈希表(Distributed Hash Table),是一 ...