Binary Tree Preorder Traversal

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?

SOLUTION1&2:

递归及非递归解法:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// sol1:
public List<Integer> preorderTraversal1(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>(); rec(root, ret);
return ret;
} public void rec(TreeNode root, List<Integer> ret) {
if (root == null) {
return;
} ret.add(root.val);
rec(root.left, ret);
rec(root.right, ret);
} public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>(); if (root == null) {
return ret;
} Stack<TreeNode> s = new Stack<TreeNode>();
s.push(root); while (!s.isEmpty()) {
TreeNode cur = s.pop();
ret.add(cur.val); if (cur.right != null) {
s.push(cur.right);
} if (cur.left != null) {
s.push(cur.left);
}
} return ret;
}
}

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/PreorderTraversal.java

LeetCode: Binary Tree Preorder Traversal 解题报告的更多相关文章

  1. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  2. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  3. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

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

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

  5. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  6. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  8. LeetCode 589 N-ary Tree Preorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回 ...

  9. [leetcode]Binary Tree Preorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...

随机推荐

  1. 对threading模块源码文件的解读(不全)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #对threading模块源码文件的解读(不全) import threading #类 #Thread() ...

  2. DevExpress添加Winform窗体到DockPanel

    在使用DevExpress过程中,原先已经创建好的导航窗体,如何添加到DockPanel中进行展示? FormX frmX = new FormX(); frmX.Show(this.DockPane ...

  3. Cocos2dx 学习记录 [2] 关于混合和高亮一些知识点的体会

    网上有一篇博客讲的是高亮的http://www.cnblogs.com/mrblue/p/3455775.html 就是这篇,尽管代码简单,但对于刚開始学习的人的我,看的还是有些吃力的,毕竟有些内容不 ...

  4. MSSQL-SQL SERVER 2008安装教程

    运行setup.exe     选择“安装”菜单,点击“全新安装或向现有安装添加功能. 环境检测通过,点击“确定”: 不用修改产品密钥,点击“下一步”: 点击“下一步”: 根据您的实际情况选择响应的组 ...

  5. Mac新建文件夹、txt文件、其他格式文件

    Mac新建txt,正好有人问我,我就把我自己的方法记录一下: 先cd到你指定的文件路径下: 新建文件夹: mkdir test 新建txt touch test.txt 新建无后缀格式文件 touch ...

  6. android通过USB使用真机调试程序

    我的机子很老,开启个android模拟器都要好几分钟,但幸亏有个android的真机,这样直接在andriod手机上调试也是一个不错的选择.下面我就介绍 一下使用android手机来调试android ...

  7. mysql 必须掌握的工具pt-query-digest安装

    mysql 必须掌握的工具pt-query-digest 古人云:工欲善其事,必先利其器.作为一名优秀的mysql dba也需要有掌握几个好用的mysql管理工具,所以我也一直在整理和查找一些能够便于 ...

  8. oracle导表小结

    事件描述:从A主机oracle服务器导出.sql文件到B主机,发现1.导入存在乱码 2.提示USERS表空没有权限(A B主机均为window系统) 1.针对第一点乱码 首先确认系统的默认字符编码GB ...

  9. PO_本地一揽子采购协议(流程)

    2014-06-04 Created By BaoXinjian

  10. fork函数相关总结

    fork的作用是根据一个现有的进程复制出一个新进程,原来的进程称为父进程(Parent Process),新进程称为子进程(Child Process).系统中同时运行着很多进程,这些进程都是从最初只 ...