原题 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的更多相关文章

  1. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  2. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  5. 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 ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 改进uboot,添加自定义快捷菜单

    .在common目录下新增cmd_menu.c文件,内容为: #include<common.h> #include<command.h> #ifdef CONFIG_MENU ...

  2. [AngularJS] Isolate State Mutations in Angular Components

    Managing state is one of the hardest things to do in any application. Angular 2 tackles this problem ...

  3. LINUX 内核2

    http://blog.csdn.net/acs713/article/category/1363650

  4. hdu1051 Wooden Sticks

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1051 大意:求最少升序序列的个数. #include <cstdio> #include &l ...

  5. SqlServer2008 之 应用积累

    1.断开数据库连接,在原有查询窗口(断开数据库连接的未关闭查询窗口),对现在所连数据库进行操作,结果是对已断开数据库的误操作. 正确操作:重新连接数据库后,应关闭原有查询窗口,新建查询窗口后再执行操作 ...

  6. Maven 仓库

    Maven仓库分为本地仓库和远程仓库(中央仓库,私服,其他公共仓库)一张图就可以很清楚的看出结构 本地仓库:本地仓库是在我们当前电脑系统盘的user-administrator-m2-reposito ...

  7. iOS之隐藏状态栏

    iOS7中,不仅应用的风格有一定的变化,状态栏变化比较大,我们可以看到UIViewController的状态栏与导航栏基本是一体的.因此UIVIEWCONTROLLER的hide/show状态的方法也 ...

  8. javaEE的十三个技术规范

    java 是一种非常棒的语言,健壮,跨平台运行,屏蔽了具体的平台环境的要求,也就是说只要支持java 虚拟机,就可以运行java程序. 下面,我们一起学习一下J2EE的十三种技术规范. 一.JDBC: ...

  9. linux - 文本处理 及 正则表达式

    先新建一个文件,并写入一些东西,方便测试, 从passwd里复制几行吧 $ /etc/passwd > passwd t$ ll 总用量 drwxrwxr-x huanghao huanghao ...

  10. python 元类——metaclass

    from stack overflow:http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python Classes ...