Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [1,2,3]
.
c++版:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<int> result;
vector<int> left;
vector<int> right; if(root == NULL) return result;
result.push_back(root->val);
left = preorderTraversal(root->left);
right = preorderTraversal(root->right); if(left.size() != )
result.insert(result.end(), left.begin(), left.end());
if(right.size() != )
result.insert(result.end(), right.begin(), right.end()); return result; }
};
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]
.
C++版本:
/**
* Definition for binary tree
* 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> ret;
dfs(root, ret);
return ret;
} void dfs(TreeNode* root, vector<int>& ret)
{
if(NULL == root)
return ;
dfs(root->left, ret);
dfs(root->right, ret);
ret.push_back(root->val);
} };
Binary Tree Preorder Traversal and Binary Tree Postorder Traversal的更多相关文章
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- java.lang.RuntimeException: Unable to start activity ComponentInfo
异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.Result ...
- [2013.9.8网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程
[2013.9.8网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程 google的android自带的apps写的是相当牛逼的,将其导入到ecli ...
- ubuntu-12.04.4-server安装
一.系统ISO下载 下载地址:http://www.ubuntu.com/download 根据自己的需求下载,我的电脑配置一般,因此选择32位的. 二.虚拟机配置 ...
- ubuntu安装XHProf
1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...
- poj 3624 Charm Bracelet 01背包问题
题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. 用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...
- 解决word转pdf后图片失真
碰到问题: 将word转pdf后图片出现失真 问题分析: 上述问题必定跟图片类型和所用软件有关,现将不同图片在不同软件下的失真情况汇总,见表1 问题解决:迫不得已,不要使用截图:若必需要用,则word ...
- Android 动画animation 深入分析
转载请注明出处:http://blog.csdn.net/farmer_cc/article/details/18259117 Android 动画animation 深入分析 前言:本文试图通过分析 ...
- wpf 如何设置滚动条在超出范围的时候才显示?(转)
VerticalScrollBarVisibility="Auto" 垂直自动显示 HorizontalScrollBarVisibility="Auto" ...
- Tomcat启动报Error listenerStart错误
http://xpenxpen.iteye.com/blog/1545648 今天启动Tomcat启动不了,报以下错: org.apache.catalina.core.StandardContext ...
- Session累计用户数据列表
OrderForm.html <body> <center> <h1 ><font size="20">Order Items& ...