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?

class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
if(!root) return result; stack<TreeNode* > treeStack;
treeStack.push(root);
TreeNode* current;
while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
result.push_back(current->val);
if(current->right) treeStack.push(current->right);
if(current->left) treeStack.push(current->left);
}
return result;
}
};

144. Binary Tree Preorder Traversal (Tree, Stack)的更多相关文章

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

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

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

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

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

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

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

  5. 【LeetCode】Binary Tree Preorder Traversal

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

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

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

  7. Binary Tree Preorder Traversal on LeetCode in Java

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

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

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

  9. 55. Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 119655 Tota ...

随机推荐

  1. CentOS 6.8 源码安装mysql 5.6

    一:卸载旧版本 rpm -qa | grep mysql rpm -e mysql #普通删除模式 rpm -e --nodeps xxx(xxx为刚才的显示的列表) # 强力删除模式,如果使用上面命 ...

  2. cocos2dx调度器(scheduler)

    调度器(scheduler) http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/m ...

  3. Go语言学习笔记 package

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 strings import "strings" strings包实现了用于操作字符的简单函数 ...

  4. 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释

    原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...

  5. wordpress域名解析到了网站,但是点击其他页面会出现ip而不是域名

         1.前提域名可以访问你的网站证明解析没问题 2.那就是wp后台的设置问题,将url和站点url改为你的域名http://www.eovision.cc清理缓存即可 亲测可用,如果改了出现页面 ...

  6. springboot注册bean失败

    启动的主类应该放在和其他包一样的目录,不能放在一个目录里面

  7. bzo1007 [HNOI2008]水平可见直线

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1007 所有可见直线形成下凸壳的样子.而且交点横坐标递增. (特殊判断平行线.但是按b从小到大 ...

  8. ubuntu 指令修改时区 tzselect

    修改时区 tzselect 指令只是根据提示一步步选择正确时区,但不能真正修改时区,最后输入提示的指令,然后重启,才能永久修改. aaron@ubuntu:~$ tzselect Please ide ...

  9. 轻量级封装DbUtils&Mybatis之一概要

    Why 一时兴起,自以为是的对Jdbc访问框架做了一个简单的摸底,近期主要采用Mybatis,之前也有不少采用Dbutils,因此希望能让这两个框架折腾的更好用. DbUtils:非常简单的Jdbc访 ...

  10. erlang的websocket例子

    创建工程 rebar-creator create-app websocket_demo 文件列表 route_helper.erl -module(route_helper). -export([g ...