【题目】

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. sql server 操作列

    新增一列 ) 修改列类型: ) 修改列的名称: EXEC sp_rename 'tableName.column1' , 'column2' (把表名为tableName的column1列名修改为co ...

  2. wingIDE Pro6 破解教程

    亲测wingIDE pro6.0.6-1激活成功 算号器下载 激活的时候选择第三项 打开算号器,获得license id 把算号器里的license id输入到第一步的输入框里 continue得到r ...

  3. php中获取中文首字母程序代码

    年会抽奖,要求一等奖的中奖概率是0.12%,二等奖中奖概率是3%,三等奖中奖概率是12%,其他中奖概率是都是谢谢惠顾. <?php /** * 抽奖 * @param int $total */ ...

  4. Delphi7打开项目提示'one or more lines were too long and has been truncated'

    打开主项目文件直接显示一排'口'形状!查了下资料也问了下伙伴,这多半应该是文件损坏了,解决办法: 1. 不关D7的事,所以重装D7应该是无效的,最好看看自己是不是有备份文件,我之前有备份的所以直接覆盖 ...

  5. WebForm母版页

    母版页:可以把界面的部分代码进行重用 添加新项-母版页 在母版页中界面代码不要写在 <asp:ContentPlaceHolder ID="head" runat=" ...

  6. h5 input 的验证

    <input type="text" id="a" required/> <input type="text" id=&q ...

  7. mybatis源码解析11---ParameterHandler解析

    ParameterHandler接口是参数处理器,位于mybatis包的org.apache.ibatis.executor.parameter下,源码如下: public interface Par ...

  8. VMware vSphere 5.x 与 vSphere 6.0各版本功能特性对比

    各版本中的新特性及功能对比:   VMware vSphere 5.0 VMware vSphere 5.1 VMware vSphere 5.5 VMware vSphere 6.0 ESXi 5. ...

  9. Determine YARN and MapReduce Memory Configuration Settings

    Determine YARN and MapReduce Memory Configuration Settings https://docs.hortonworks.com/HDPDocuments ...

  10. 【题解】Luogu P2730 魔板

    蒟蒻的第一道蓝题--好像也没有蓝的程度 一篇无STL的超弱题解(入门写法无误了QAQ 传送门 很经典的一道BFS 这是初始状态. 操作A 操作B 操作C 思路1 不使用cantor展开的情况 1. 对 ...