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].

二叉树的前序遍历,根节点→左子树→右子树

解题思路一:

递归实现,JAVA实现如下:

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

解题思路二:

使用stack实现,JAVA实现如下:

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

Java for LeetCode 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. leetcode 144. Binary Tree Preorder Traversal ----- java

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

  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. AU3学习资源

    AU3中文站:http://www.autoitx.com/

  2. TYVJP1933 绿豆蛙的归宿

    背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达 ...

  3. hihocoder #1270 建造基地

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...

  4. eclipse中建python项目并运行

    1. Help → Install New Software 2.Enter http://pydev.org/updates 3.点击Click "Next" and " ...

  5. Linux cscope命令

    一.简介 Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置.被调用的位置等信息.Cscope对 C /C++支持 ...

  6. 在不借助其他工具的情况下破解Windows开机密码

    文章:http://www.cnblogs.com/vforbox/p/4828855.html#!comments. 从该文章我们也可以得到一个快速启动某个程序的方法:将自己常用的程序命名为seth ...

  7. JavaScript input file上传前获取文件名、文件类型、文件大小等信息

    document.getElementById("productImgInput").files[0].type document.getElementById("pro ...

  8. 让scrollView、tableView滚动到底部

    - (void)scrollsToBottomAnimated:(BOOL)animated { CGFloat offset = self.tableView.contentSize.height ...

  9. 锋利的jQuery-7--编写插件基础知识

    插件的基本要点: 1.命名推荐:jquery.[插件名].js,避免和其他js库插件混淆. 2.对象方法附加到:jQuery.fn上,全局函数附加到:jQuery对象本身. 3.在插件内部,this指 ...

  10. 锋利的jQuery-1--end()

    1.导航菜单:选中后显示当前标签下的子标签并且高亮,并且隐藏其他同级标签,(一行级联操作即完成),主要看end()函数用法. end(): 回到最近的一个"破坏性"操作之前.就是指 ...