【题目】

Given a binary tree, return the preordertraversal of its nodes' values.

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [1,2,3]

【思路】

有参考,好机智,使用堆栈压入右子树,暂时存储。

左子树遍历完成后遍历右子树。

【代码】

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
LinkedList<Integer> ans=new LinkedList<Integer>();
Stack<TreeNode> tmp=new Stack<TreeNode>();
while(root!=null){
ans.add(root.val);
if(root.right!=null){
tmp.push(root.right);
}
root=root.left;
if(root==null&&!tmp.isEmpty()){
root=
tmp.pop();
}

}
return ans;
}
}

[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal的更多相关文章

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

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

  2. [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal

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

  3. LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

  4. 【Leetcode】【Medium】Binary Tree Preorder Traversal

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

  5. 【leetcode刷题笔记】Binary Tree Preorder Traversal

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

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

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

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

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

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

  9. 【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. 51Nod 1085 背包问题 (01背包)

    在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. 收起   输入 第1行,2个 ...

  2. 防火墙iptables 设置

    在服务器上架了一个tomcat,指定好端口号,我就开始访问,未果! 公司对服务器(RedHat)端口限制,可谓是滴水不漏! 用iptables 查看防火墙设置: Shell代码 iptables -n ...

  3. AUTEL MaxiSYS Pro MS908P Diagnostic System with WiFi Update Online

    The MaxiSYS? Pro has been designed to be the go-to tool for the professional technician who performs ...

  4. webpack学习入门

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...

  5. zabbix部署相关

    一.centos7 安装zabbix 二.zabbix 乱码问题 三.zabbix自动发现自动注册 四.zabbix3.4实现sendEmail邮件报警

  6. jQuery安装和语法

    jQuery是一个JavaScript函数库,可实现HTML元素选取及操作.CSS 操作.HTML事件函数.JavaScript特效和动画.HTML DOM遍历和修改.AJAX等功能. 在html中引 ...

  7. Docker+Jenkins+Maven+SVN搭建持续集成环境

    Docker+Jenkins+Maven+SVN搭建持续集成环境 环境拓扑图(实验项目使用PHP环境) 发布流程图 环境说明 系统:Centos 7.4 x64 Docker版本:18.09.0 Ma ...

  8. ubuntu 16.04 tip

    参考 1. 安装 python3.6 sudo add-apt-repository ppa:jonathonf/python-3.6  sudo apt-get update sudo apt-ge ...

  9. Golang实现杨辉三角

    杨辉三角,也算是一个经典的题目了.就简单的说说. 写代码之前,先分析要做的东西的特点,找到规律,再把这个规律描述一下. 然后把这个描述翻译成编程语言,就可以说是编程了. 那么杨辉三角有什么特点? 首先 ...

  10. UI自动化(十二)appium

    windows不可以测试iosmac 是可以测试Android ios appium cmd 下装的是appium的服务端appium-desktop 是定位元素的工具,同时自带一个appium服务端 ...