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

 /**
* Definition for a binary tree node.
* 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> res;
help(root,res);
return res;
}
void help(TreeNode *root,vector<int> &res)
{
if(root!=NULL)
{
res.push_back(root->val);
help(root->left,res);
help(root->right,res); }
}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> res;
stack<TreeNode* > s;
s.push(root);
while(!s.empty())
{
TreeNode *now=s.top();
s.pop();
if(now!=NULL)
{
res.push_back(now->val);
s.push(now->right);
s.push(now->left);
}
}
return res;
} };

Binary Tree Preorder Traversal的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

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

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

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

  3. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

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

  10. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

随机推荐

  1. Python 面向对象[Day 06]

     面向对象编程(Object-Oriented Programming)  概述 面向过程:根据业务逻辑从上至下写代码,实现所需功能. 函数式:将某些功能代码封装至函数中,在需要时调用函数,函数式代码 ...

  2. ArcEngine拓扑

    空间拓扑描述的是自然界地理对象的空间位置关系-相邻,重合,连通等,是地理对象空间属性的一部分.目前ESRI提供的数据存储方式中,Coverage和GeoDatabase能够建立拓扑,Shape格式的数 ...

  3. 【转】Sublime Text3注册码(可用)

    补充:2015.11.05日经过测试,3个注册码都是可用的,不会用的请百度用法... 最近觉得Sublime Text3比Notepad++好使,可惜需要购买,于是网上搜了一下,屌丝的福音啊: Sub ...

  4. nginx rewrite

    # nginx rewrite rule #Main menu rules.rewrite ^(.*?)/index.html$ $1/index.php?r=site/index break;rew ...

  5. IP地址及其子网划分

    说实话,弄到子网划分的时候还是及其头晕的,又是这又是那的,现在我们来讲解一下这些东西, 首先我们来介绍一下IP地址,要弄清子网划分,子网掩码首先还是要弄清IP地址的划分 IP地址是给Internet上 ...

  6. jQuery--事件总结

    标准的绑定: bind(type,[,data],fn)==>第一个参数是事件类型 第二个可选参数作为event.data 传递给事件对象的额外数据对象 第三个参数为用来绑定的处理函数 简写绑定 ...

  7. http状态消息

    1-5状态码了解 1XX 表示信息(消息) 2XX 表示成功 3XX 表示重定向 4XX 表示请求错误 *** 5XX 表示服务端错误 常见状态码 200 请求成功 一切正常 301 重定向,修改后的 ...

  8. SharePoint 2013 CSOM 对象模型属性包

     博客地址:http://blog.csdn.net/FoxDave 虽说是翻译,但是并没什么翻译的,主要内容就是说有一些能通过Server API配置的Site/Web属性在CSOM中找不到,其 ...

  9. 解决ORA-14450:试图访问已经在使用的事务处理临时表

    select * from dba_objects where object_name='TEMP_MZGL_BRSFD'; select 'alter system kill session ''' ...

  10. Spring事务配置的五种方式(转载)

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...