题目:

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

Note: Recursive solution is trivial, could you do it iteratively?

代码:

/**
* 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> ret_l, ret_r;
if ( root ){
ret_l = Solution::preorderTraversal(root->left);
ret_l.insert(ret_l.begin(), root->val);
ret_r = Solution::preorderTraversal(root->right);
ret_l.insert(ret_l.end(), ret_r.begin(), ret_r.end());
}
return ret_l;
}
};

tips:

trivial的递归版

===================================

再来一个non trivial的迭代版

/**
* 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> ret;
if ( !root ) return ret;
stack<TreeNode*> sta;
sta.push(root);
while ( !sta.empty() )
{
TreeNode *tmp = sta.top();
sta.pop();
ret.push_back(tmp->val);
if (tmp->right) sta.push(tmp->right);
if (tmp->left) sta.push(tmp->left);
}
return ret;
}
};

tips:

把递归调用改成人工堆栈:

1. 把根节点压进栈

2. 栈定元素出栈,把val加入到ret中

3. 注意:先压right入栈,再压left入栈(保证压入栈的元素都是非NULL)

循环1~3,直到栈空。

==============================================

还有更高端一些的Morris遍历算法:特点是空间复杂度是O(1)

http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html

=================================================

第二次过这道题,只写了一个递归的。

/**
* 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> ret;
Solution::traversal(ret, root);
return ret;
}
static void traversal(vector<int>& ret, TreeNode* root)
{
if (!root) return;
ret.push_back(root->val);
Solution::traversal(ret, root->left);
Solution::traversal(ret, root->right);
}
};

【Binary Tree Preorder Traversal】cpp的更多相关文章

  1. 【遍历二叉树】01二叉树的前序遍历【Binary Tree Preorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的前序遍历的 ...

  2. 【Binary Tree Inorder Traversal】cpp

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  3. 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...

  4. 【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的中序遍历的 ...

  5. 【LeetCode】Binary Tree Preorder Traversal

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

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

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

  7. 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

    [144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

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

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

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

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

随机推荐

  1. 怎样在Android中ListView与ArrayAdapter配合使用

    [代码]main.xml   <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  2. ubuntu 12.04 64位设置兼容32位的实现

    在ubuntu12.04上,要运行32的程序,需要安装32位的兼容库. 以前在10.04上成功安装过,方法是 sudo apt-get install ia32-libs 但是在12.04上遇到了困难 ...

  3. HTTP请求状态类

    <?php /** * 常用常量文件 * */ /** * HTTP协议请求状态 */ class HttpRequest { //100类 ----用于指定客户端应相应的某些动作---- co ...

  4. 添加删除程序无法安装IIS 提示没法加载模块

    添加删除程序无法安装IIS 提示没法加载模块 安装iis的时候提示 解决办法:依次是 属性--高级--系统变量--Path  变量值是:%SystemRoot%\system32;%SystemRoo ...

  5. php设计模式之单例、多例设计模式

    单例(Singleton)模式和不常见的多例(Multiton)模式控制着应用程序中类的数量.如模式名称,单例只能实例化一次,只有一个对象,多例模式可以多次实例化. 基于Singleton的特性,我们 ...

  6. php header 函数详解

    网页的缓存是由 HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must- revalidate等,默认为private.其作用 ...

  7. Lambda Grinding Miller From Zenith

    data = """ The Basic Things About Grinding Mill A grinding mill is a unit operation d ...

  8. 7-ZIP实现数据高度压缩

    From:http://www.cnblogs.com/killerlegend/p/3746395.html Author:KillerLegend Date:2013.5.22 选中文件,鼠标右键 ...

  9. web.xml中常见配置解读

    文章转自:http://blog.csdn.net/sdyy321/article/details/5838791 有一般XML都必须有的版本.编码.DTD <web-app>下子元素&l ...

  10. DevExpress汉化(WinForm)

    /* *隔壁老王原创,2013-09-21,转载请保留本人信息及本文地址. *本文地址:http://wallimn.iteye.com/blog/1944191 */ 最简单的方式就是使用汉化资源, ...