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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- delphi 动态建立WebBrower
//Delphi动态建立WebBrowerunit Main;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphi ...
- 为TListBox添加水平滚动条
为TListBox添加水平滚动条 实例说明 TListBox组件是一个较为常用的列表组件,在默认情况下该组件是没有水平滚动条的,所以文字过长会显示不完全,在文字较短的情况下还可以,但是如果一行的文字很 ...
- Android开发学习之 定制界面风格
统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).OPhone系统提供了很多系统默认的风格和主题,但是很多情况下,这些不能满足我们的需 ...
- [ES6] 16. Object Enhancements
Define object: var color = "blue"; var speed = 120; var car = {color, speed}; console.log( ...
- Xcode无法设置视图的 autosizing control原因
转自:Xcode无法设置视图的 autosizing control原因 学习Xcode的iOS编程时,可能会发现Autosizing Control不见了,其原因很简单,因为你在设置中选择了Auto ...
- 类型查找器 ITypeFinder
NopCommerce为了支持插件功能,以及支持一些自动注册的功能.系统提供了类型查找器.ITypeFinder以及实现类就是提供此功能.通过类型查找器可以查找本程序域中的类,也可以查找整个bin目录 ...
- IDEA 编译错误:java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)
错误描述 在用IDEA编译别人的项目的时候遇到下面的错误: java: try-with-resources is not supported in -source 1.6 (use -source ...
- Qt focusoutevent 不响应的解决方法
一般利用focus(焦点)来实现弹窗自动关闭效果. Qt的focus貌似是自己的bug, 经常无法接收到focusout的事件 例如: widgetA 中执行 widgetB->show(); ...
- 在openshift上使用django+postgresql
openshift上用的是django 1.7,数据库选择的是postgresql 9.2 本地开发用的是sqlite3数据库,发布到openshift上后是没有数据的(本地的sqlite3数据库里的 ...
- oracle10g 和oracle11g同时安装时PL/SQL连不上解决方案
oracle10g 和oracle11g同时安装的时候,PL/SQL连不上解决办法:找到两者的配置文件改成一致 oracle10g服务端和oracle11g客户端同时安装的时候,PL/SQL连不上解决 ...