3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal
没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊..
总之,前序、中序、后序,是按照根的位置来决定的,根首先访问,是前序。
/**
* 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) {
vector<int> r;
if (root == NULL) return r; r.push_back(root->val); if (root->left != NULL)
{
vector<int> v = preorderTraversal(root->left);
r.reserve(r.size() + distance(v.begin(),v.end()));
r.insert(r.end(),v.begin(),v.end());
} if (root->right != NULL)
{
vector<int> v = preorderTraversal(root->right);
r.reserve(r.size() + distance(v.begin(),v.end()));
r.insert(r.end(),v.begin(),v.end());
}
return r;
}
};
另外这样写,有大量的Vector复制操作,太浪费了,其实一个全局的int数据就搞定了,C++这样写还是有点麻烦。
3月3日(3) Binary Tree Preorder Traversal的更多相关文章
- 3月3日(4) Binary Tree Inorder Traversal
原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- GLSL实现简单硬件Anisotrop Lighting 【转】
http://blog.csdn.net/a3070173/archive/2008/11/13/3294660.aspx 各向异性光照往往用于处理一些具有各向异性表面的物体,如:光盘的盘面.为避免在 ...
- JavaScipt call和apply用法
转:http://www.cnblogs.com/wupeng/p/3477879.html Javascript call与apply记录 [注]:记录自己对javascript中call与appl ...
- 初步认知MySQL metadata lock(MDL)
http://blog.itpub.net/26515977/viewspace-1208250/ 概述 随着5.5.3引入MDL,更多的Query被“Waiting for table metada ...
- Jquery Validate根据其他元素的事件来触发单个元素的异步校验
场景:在做一个车辆信息管理模块,而车牌是通过车牌颜色和车牌号码来确定唯一性的,录入车牌信息时需对车牌进行唯一性校验.
- 笔记——Function类型 及其 call、apply方法
每个函数都是Function类型的实例.函数有三种定义方式和两个内部属性arguments和this. 同时函数也是对象,也有属性和方法.本篇主要其call()和apply()方法 属性 length ...
- FZU 1686 神龙的难题 (DLX)
神龙的难题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【优先队列】【最近连STL都写不出来了/(ㄒoㄒ)/~~】hdu_5360/多校#6_1008
题意:就是让你写出一个邀请朋友的顺序,朋友答应一起出去玩的条件是除他以外所有同意出去玩的人数要在[ l[i], r[i] ]范围内,否则他就不答应. 分析:这题比赛的时候想麻烦了,其实只要在左边界满足 ...
- 在Android中动画移动一个View的位置,采用Scroller类实现Android动画之 View移动
在Android中动画移动一个View的位置,采用Scroller类实现 今天说最近自己遇到的一个问题,就是要用动画效果来移动一个VIew的位置. 这个具体的情况是,需要做一个SlidingMenu的 ...
- 转:艾瑞咨询2016 IM云的发展趋势
转自: http://www.cnblogs.com/lingyunhu/p/rtc63.html
- Ext.Slider的试用小记
最近需要做一个滑动条,动态改变地图动画播放的速度.实在没有自己用js写一个的那个水平,于是找到了Ext.Slider,ExtJS的版本是3.3.0. new Ext.Slider({ id: 'pla ...