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?

题目大意:非递归方式实现二叉树的前序遍历。

解题思路:自己实现一个栈,循环(根节点直接入栈并且入res List,循环入栈左子树,pop栈,入栈右子树)。

public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while(curr!=null||!stack.isEmpty()){
while(curr!=null){
stack.push(curr);
res.add(curr.val);
curr=curr.left;
}
curr = stack.pop();
curr = curr.right;
}
return res;
}
}

Binary Tree Preorder Traversal —— LeetCode的更多相关文章

  1. Binary Tree Preorder Traversal leetcode java

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

  2. Binary Tree Preorder Traversal -- leetcode

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  3. Binary Tree Preorder Traversal -- LEETCODE 144

    方法一:(迭代) class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ...

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

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

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

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

  6. 【LeetCode】Binary Tree Preorder Traversal

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

  7. Binary Tree Preorder Traversal on LeetCode in Java

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

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

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

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

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

随机推荐

  1. 关于SqlServer修改数据库常用信息的方法

    --系统表里存放各个数据库属性信息的表之一SELECT name AS [Logical Name], physical_name AS [DB File Path],type_desc AS [Fi ...

  2. U3D 通过预置对象实现手动创建精灵

    一: 这种可以在游戏的一开始,不显示某些物体,而且通过某种时机,来显示所需要显示的物体 这里就用到了实例化预置对象. 实例化更多通常用于实例投射物(如子弹.榴弹.破片.飞行的铁球等),AI敌人,粒子爆 ...

  3. android EventBus 的使用

    今天简单的介绍 一下啊 android  EventBus 的使用 EventBus 在官方介绍中是订阅......什么的 一大堆  ,  在我android 菜鸟眼里 就是用来代替android 广 ...

  4. onContextItemSelected 用法

    http://blog.csdn.net/kavensu/article/details/8045041 onCreateOptionsMenu :此方法为创建菜单方法,这个菜单就是你在点击手机men ...

  5. 创建 window service 定时任务

    参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...

  6. 绘图quartz之阴影

          //设置矩形的阴影  并在后边加一个圆 不带阴影     步骤:     CGContextRef context = UIGraphicsGetCurrentContext();     ...

  7. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

  8. this的一个作用 当前对象

    class Person{ String name="小花"; int age=19; void eat(){  System.out.println("在吃饭" ...

  9. elastic search 学习笔记

    Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...

  10. jQuery慢慢啃之选择器(二)

    1.$("#myDiv");ID匹配一个元素 <span id="foo[bar]"></span> $("#foo\\[ba ...