Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来、不屑一顾的题目罢。然而因为本人记性不好、基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人于己皆有帮助。
估计能看到这里的读者,都是见过题目的,但还是链接原题在此:Binary Tree Preorder Traversal
二话不说,直接上代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
} TreeNode curr = root;
Stack<TreeNode> rights = new Stack<TreeNode>();
while (curr != null) {
res.add(curr.val);
if (curr.right != null) {
rights.push(curr.right);
}
curr = curr.left;
if (curr == null && !rights.empty()) {
curr = rights.pop();
}
} return res;
}
}
一次过,372 ms,C++ 大牛勿笑。。。Java 大牛也包涵。。。
做出来之后想搜搜有木有(基本上肯定有)更高级的解法,于是发现了教科书一般的景象。。。无论是 C++ 还是 Java,代码总有相同的一处,就是根节点入栈,出栈之后再判断左右孩子是否为空。本人猜想这一定是某本或者某些教科书里的标准解法,被大家铭记在心了。
教科书解法或许更有教学意义,或许更普适通用,但本人的小解法,用来对付这道题已经足够。
前序遍历只需保存当前节点的右孩子,当前节点不用在栈里打个来回,直接痛快把自己的值交出来,然后把皇位让给左孩子便是了~
不过后面一道后序遍历的还木有做出来。。。继续努力!
大家光棍成双节快乐!
Binary Tree Preorder Traversal on LeetCode in Java的更多相关文章
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- R文件丢失异常原因汇总
引言: R文件丢失异常在java开发中是个比较常见的异常,造成这个异常的原因可能非常微小,但是给Android开发者们造成的麻烦可是巨大的,当程序员们费尽千辛万苦,找到自己错在哪里的时候,绝对会对自己 ...
- php函数serialize()与unserialize()
serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a value ser ...
- Android布局管理器(表格布局)
表格布局有TableLayout所代表,TableLayout继承了LinearLayout,因此他的本质依然是LinearLayout. 表格布局采用行.列的形式来进行管理,在使用的时候不需要声明多 ...
- linux下启动oracle服务命令
以redflag(redhat /centos)linux下的 oracle 10g 为例: 如果oracle安装和配置都没有问题的话: 依次执行以下代码即可启动oracle服务. #su - ora ...
- js对象字符串互转
利用原生JSON对象,将对象转为字符串 var jsObj = {}; jsObj.testArray = [1,2,3,4,5]; jsObj.name = 'CSS3'; jsObj.date = ...
- iOS开发之指定UIView的某几个角(小于4)为圆角
在iOS开发中,我们经常会遇到View设置圆角的问题,如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore ...
- [转]mysql导出导入中文表解决方法
在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...
- set,multiset容器类型
set和multiset会根据特定的排序准则,自动将元素排序.两者不同处在于multiset允许元素重复而set不允许. 一.集和多集(set 和multiset 容器类) 在使用set和multis ...
- Mac开机黑屏解决办法
开机黑屏问题 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !import ...
- TortoiseGit(乌龟git)保存用户名密码的方法(转)
转自:http://my.oschina.net/jjyuangu/blog/232798?p=1 windows下比较比较好用的git客户端有2种: 1. msysgit + TortoiseGit ...