[LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看:
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二叉树前序遍历的更多相关文章
- 144. Binary Tree Preorder Traversal (二叉树前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
随机推荐
- 3D显微镜笔记
1. 三视图:能够正确反映物体长.宽.高尺寸的正投影工程图(主视图,俯视图,左视图三个基本视图)为三视图,这是工程界一种对物体几何形状约定俗成的抽象表达方式. 附上自己大二时候设计的减速器--设计了两 ...
- 用了Dapper之后就不要再见到SqlConnection咯
一.背景 前几天看公司一个新项目的底层使用了dapper,大家都知道dapper是一个非常强大的半自动化orm,帮程序员解决了繁琐的mapping问题,用起来非常爽,但我还是遇到了一件非常不爽的事情, ...
- PriorityQueue 优先队列的实现
PriorityQueue 的 implementation PriorityQueue即是优先队列.通俗的说就是体育课的时候老师要求从高到低排序,老师能直接一眼看出谁是最高的在班级里.当这个最高的离 ...
- ansible playbook 安装docker
1.新增host配置到/etc/ansible/hosts文件中 [docker] 192.168.43.95 2.配置无密码登录 # 配置ssh,默认rsa加密,保存目录(公钥)~/.ssh/id_ ...
- 九. Vuex详解
1. 理解Vuex 1.1 Vuex功能 官方解释 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用 集中式存储 管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方 ...
- PyQt(Python+Qt)学习随笔:QTableWidget项编辑方法editItem、openPersistentEditor
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.触发编辑项的editItem方法 QTableWidget提供了触发项编辑的方法,调用语法如下: ...
- PHP代码审计分段讲解(7)
17 密码md5比较绕过 <?php if($_POST[user] && $_POST[pass]) { mysql_connect(SAE_MYSQL_HOST_M . ': ...
- 软件测试相关术语(测试策略 && 测试方案 ....)
软件测试有几种不同的定义方法: a.软件测试是为了发现程序中的错误而执行程序的过程. b.软件测试是根据软件开发各阶段的规格说明和程序的内部结构而精心设计的一批测试用例,并运用这些测试用例运行程序,以 ...
- jQuery无限滚动
由于demo实在虚拟桌面写的,所以懒得在敲一遍了,直接贴图了
- 在iframe中获取另一个iframe中的元素
$(top.parent.iframeId).contents().find("#selector") //iframeId为iframe的id名称