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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) { Deque<TreeNode> queue = new ArrayDeque<TreeNode>(); List list = new ArrayList<Integer>(); if( root == null )
return list; queue.add(root); while( !queue.isEmpty() ){ TreeNode node = queue.poll();
list.add(node.val); if( node.right != null )
queue.addFirst(node.right);
if( node.left != null )
queue.addFirst(node.left); } return list; }
}

leetcode 144. Binary Tree Preorder Traversal ----- java的更多相关文章

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

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

  2. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  3. Java for LeetCode 144 Binary Tree Preorder Traversal

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

  4. Java [Leetcode 144]Binary Tree Preorder Traversal

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

  5. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  6. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  7. Leetcode 144 Binary Tree Preorder Traversal 二叉树

    二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...

  8. Leetcode 144. Binary Tree Preorder Traversal

    参考例子:[8,3,1,6,4,7,10,14,13] 8,3,1 和 6,4 说明从root开始,沿着左臂向下寻找leaf 的过程中应该逐个将node.val push入ans. class Sol ...

  9. LeetCode 144. Binary Tree Preorder Traversal 动态演示

    先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...

随机推荐

  1. Matlab基础

    基本运算: 一,矩阵的生成 clc ; clear all; close all; 1.直接输入 A = [ 1 ,2 ,3,4;2,3,4,5;3,4,5,6] A = 1 2 3 4 2 3 4 ...

  2. ie9,10 uploadify cleanUp bug

    起因:ie多次加载uploadify3.2版本这个组件的时候,出现了SCRIPT5007: 缺少对象.  From:http://blog.163.com/xiangfei209@126/blog/s ...

  3. 如何登录Google美国服务器

    Google访问须知: ① 先访问一次 https://www.google.com/ncr ,禁止“国家重定向(No country Redirect) ” ② 再点击右上角齿轮图标,选第一项“Se ...

  4. 在线体验K2 BPM微信审批

    “微信审批”在江湖中传言已久,但很多人依然“只闻其声,未见其人”,这传说中的手感到底有多好?今天,我们就一起来揭开它的真面目吧. 故事发生在上周六傍晚,我接到了加班电话. 晚上21:30终于加完班了, ...

  5. Css杂谈

    CSS是Cascading Style Sheets(级联样式表)的缩写. CSS涉及字体.颜色.边距.高度.宽度.背景图像.高级定位等方面. HTML可以用于为网站添加布局效果,但有可能被误用.而C ...

  6. AIX查看内存卡槽

    1.lscfg -vp|grep Processor 2.lscfg -vp|grep -p Memory

  7. T420修改wifi灯闪动模式

    给T420新装了centos7发现默认的配置wifi灯是工作时闪动的,有点晃眼,想改成简单的on 的时候常亮,off的时候常暗的模式 添加配置文件: vi /etc/modprobe.d/wlanle ...

  8. .net 小技巧

    简单提示效果: <input runat="server" type="text" id="SelPerson" value=&quo ...

  9. dllimport路径问题

    今天做了个试验,是针对dllimport("XXX.DLL");这样写的时候,系统是如何寻找该dll的. 首先系统会搜寻主应用程序根目录. 其次搜寻操作系统安装目录,一般情况是C: ...

  10. stm32启动文件 startup_stm32f10x_hd.s

    ;* 文件名          : startup_stm32f10x_hd.s;* 库版本           : V3.5.0;* 说明:             此文件为STM32F10x高密度 ...