题目:

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?

题解:

递归做法如下:

 1     public void helper(TreeNode root, ArrayList<Integer> re){
 2         if(root==null)
 3             return;
 4         re.add(root.val);
 5         helper(root.left,re);
 6         helper(root.right,re);
 7     }
 8     public ArrayList<Integer> preorderTraversal(TreeNode root) {
 9         ArrayList<Integer> re = new ArrayList<Integer>();
         if(root==null)
             return re;
         helper(root,re);
         return re;
     }

非递归方法:

 1 public ArrayList<Integer> preorderTraversal(TreeNode root) {
 2     ArrayList<Integer> res = new ArrayList<Integer>();
 3     if(root == null)
 4         return res;
 5     LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
 6     while(root!=null || !stack.isEmpty()){
 7         if(root!=null){
 8             stack.push(root);
 9             res.add(root.val);
             root = root.left;
         }
         else{
             root = stack.pop();
             root = root.right;
         }
     }
     return res;
 }

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

  1. Binary Tree Postorder Traversal leetcode java

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

  2. Binary Tree Preorder Traversal —— LeetCode

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

  3. Binary Tree Preorder Traversal -- leetcode

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

  4. Binary Tree Inorder Traversal leetcode java

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

  5. Binary Tree Preorder Traversal -- LEETCODE 144

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

  6. Binary Tree Preorder Traversal on LeetCode in Java

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

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

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

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

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

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

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

随机推荐

  1. Java设计模式GOF之工厂模式

    一.工厂模式(Factory) 1.实现了创建者和调用者的分离 2.应用场景 ①JDK中 Calendar 的 getInstance(): ②JDBC 的 Connection 对象的获取: ③Hi ...

  2. Questions(Updating)

    有时候做题时会遇到一些未学习的零碎知识点,或存疑的疑惑 为防止遗忘,在此记录 1.复数除法与线性变换的关系 Accepted Codeforces 8D(2018.5.9) Definition: 复 ...

  3. POJ 2778 DNA Sequence(AC自动机+矩阵)

    [题目链接] http://poj.org/problem?id=2778 [题目大意] 给出一些字符串,求不包含这些字符串的长度为n的字符串的数量 [题解] 我们将所有串插入自动机计算match,对 ...

  4. 利用Pastezort渗透win7

    下载Pastezort git clone https://github.com/ZettaHack/PasteZort.git 给Pastezort文件夹提升权限 /root/PasteZort/ ...

  5. poj 2777 线段树 区间更新+位运算

    题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4  板长 颜色数目 询问数目C 1 1 2P ...

  6. python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

    我使用的python版本为:3.3.2 如果你对python中tkinter模块的菜单操作不是很了解,你可以看看: python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推 ...

  7. ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

    ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  8. Opencv各版本的配置

    OpenCV 2.49 + VS2012 配置指南 首先下载 OpenCV2.4.9 源码: 一.  配置系统环境变量 1.  将源码解压到指定目录: 2.  将 opencv 添加到系统环境变量: ...

  9. C#高级编程9 第11章 Linq

    Linq 1.Linq概述 列表和实体 准备数据: public class Championship { public int Year { get; set; } public string Fi ...

  10. 双频无线网安装设置(5g ) for linux

    为了在局域网实现远程wifi调试,例如调试需要图像数据传输,则需要搭建局域网5g无线网络. 1.硬件要求 a. TP-Link(型号:TL-WDR6500,AC1300双频无线路由器,支持5g,2.4 ...