题目链接

题目大意:返回二叉树的先序遍历list。中序见94,后序见145。

法一:普通递归遍历,只是这里多了一个list数组,所以分成了两个函数。代码如下(耗时1ms):

     public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
list = dfs(root, list);
return list;
}
public static List<Integer> dfs(TreeNode root, List<Integer> list) {
if(root == null) {
return list;
}
else {
list.add(root.val);
list = dfs(root.left, list);
list = dfs(root.right, list);
return list;
}
}

法二(借鉴):先序非递归。代码如下(耗时1ms):

     public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
List<Integer> list = new ArrayList<Integer>();
while(tmp != null || !stack.isEmpty()) {
//将所有左孩子压栈,直到没有左孩子,并且由于是先序遍历,所以在压左孩子的时候就放入结果list中
while(tmp != null) {
list.add(tmp.val);
stack.push(tmp);
tmp = tmp.left;
}
//如果左孩子压完了,就访问右孩子
if(!stack.isEmpty()) {
tmp = stack.pop();
tmp = tmp.right;
}
}
return list;
}

中序非递归

     public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while(tmp != null || !stack.isEmpty()) {
while(tmp != null) {
stack.push(tmp);
tmp = tmp.left;
}
//与先序不同的是,在弹出时放入结果list
if(!stack.isEmpty()) {
tmp = stack.pop();
list.add(tmp.val);
tmp = tmp.right;
}
}
return list;
}

144.Binary Tree Preorder Traversal---二叉树先序、中序非递归遍历的更多相关文章

  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. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

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

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

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

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

  6. Leetcode 144 Binary Tree Preorder Traversal 二叉树

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

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

    给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3],   1    \     2    /   3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...

  8. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

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

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

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

随机推荐

  1. solrCloud源码分析之CloudSolrClient

    CloudSolrClient是solrj提供的客户端与solrCloud交互的类.该类的实例与zookeeper进行通信来确定solrCloud collections中的solr endpoint ...

  2. bzoj4032-最短不公共子串

    题意 给出两个长度小于等于2000的小写字母串,四个问题: A的最短子串不是B的子串 A的最短子串不是B的子序列 A的最短子序列不是B的子串 A的最短子序列不是B的子序列 分析 虽然求的是不公共,但是 ...

  3. 使用 openssl 生成证书(转)

    一.openssl 简介 openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用.健壮.功能完备的工具套件,用以支持SSL/TLS 协议的实现.官网:https://www.openss ...

  4. Stanford机器学习---第十四讲.机器学习应用举例之Photo OCR

    http://blog.csdn.net/l281865263/article/details/50278745 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归.Oc ...

  5. Codeforces Round #340 (Div. 2) E 莫队+前缀异或和

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. 3.UiObejct API 详细介绍

    一.点击与长按 1.组件区域位置关系: Rect 对象代表一个矩形区域:[left,Top][ARight,Bottom](即左上角图标到右下角图标) 2.点击与长按相关API: 返回值 API 说明 ...

  7. discuz uc_server 配置登录

    新运行uc_server环境,先配置好ucenter链接-----这部很重要,我从新环境中安装下载的discuz代码,这部没配置,密码又不知道,怎么更改调试,都不起作用,在框架中,跳转到了原来线上的u ...

  8. 给上传文件的input控件“美容”

    在工作中经常会遇到form表单这种东西.然而表单的其他input控件样式还是很好改变的.但是,唯独input类型是file的文件上传控件可能就没那么好打扮的漂亮. demo: html代码 <b ...

  9. Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431953239 ...

  10. Spark RDD中的aggregate函数

    转载自:http://blog.csdn.net/qingyang0320/article/details/51603243 针对Spark的RDD,API中有一个aggregate函数,本人理解起来 ...