关于二叉树的遍历请看:

http://www.cnblogs.com/stAr-1/p/7058262.html

/*
考察基本功的一道题,迭代实现二叉树前序遍历
*/
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root==null)
return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty())
{
TreeNode cur = stack.pop();
res.add(cur.val);
if (cur.right!=null)
stack.push(cur.right);
if (cur.left!=null)
stack.push(cur.left);
}
return res;
}

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

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

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

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

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

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

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

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

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

  5. Leetcode 144 Binary Tree Preorder Traversal 二叉树

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

  6. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

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

  7. LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)

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

  8. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

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

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

随机推荐

  1. PHP AES加密封装类

    简介 PHP AES 加密解密常用封装类 使用方式 $key = 123; $aes = new Aes($key); $data = ['a' => 1]; $aes->decrypt( ...

  2. 真香,理解记忆法学习Python基础语法

    这篇文章很难写!我最开始学 Python,和大多数人一样,是看的菜鸟教程: 在写完这篇文章的第一遍后,我发现并没有写出新意,很可能读者看到后,会和我当初一样,很快就忘了.我现在已经不是读者而是作者了, ...

  3. Python的富比较方法__lt__、__gt__之间的关联关系分析

    Python的富比较方法包括__lt__.__gt__分别表示:小于.大于,对应的操作运算符为:"<".">".那么是否象普通数字运算一样,这两个方 ...

  4. PyQt(Python+Qt)学习随笔:QMdiArea多文档界面区域的viewMode、documentMode、tabsClosable、tabPosition等属性介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 viewMode属性用于控制子窗口是使用子窗口模式(QMdiArea. ...

  5. PyQt(Python+Qt)学习随笔:QStandardItemModel指定行和列创建模型后的数据项初始化的两种方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QStandardItemModel通过构造方法 QStandardItemModel(int ro ...

  6. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的按钮改变缺省文字的方法

    在Qt Designer中可以预先定义标准按钮,相关支持的标准按钮请见<PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButton ...

  7. PyQt(Python+Qt)学习随笔:布局控件layoutStretch属性

    在Qt Designer中布局控件有4个,分别是Vertical Layout(垂直布局).Horizontal Layout(水平布局).Grid Layout(网格布局).Form Layout( ...

  8. [MRCTF2020]Ezpop

    题目: Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From https://ctf.ieki ...

  9. SSH无密码登陆

    http://blog.chinaunix.net/uid-26284395-id-2949145.html

  10. MacOS上Parallels Desktop安装MacOSHighSierra

    下载dmg文件 http://www.macoshome.com/macos/977.html 配置宿主机时间2015-10-27 23:25 为了防止"macOS未能安装在您的电脑上_安装 ...