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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- 关于SQL IO的一些资料
前些天在做优化的时候发现一个有意思的现象,单纯的SQL执行很快,秒级返回,但是页面响应却很慢,一直在想这是为什么呢?有点怀疑服务器的IO有问题,想了想做了个实验,模拟了同样的场景,通过优化SQL将 ...
- 项目中常用SQL语句总结
1.项目中常常需要修改字段长度,但需要保留数据--增加业务受理 项目名称 字段长度alter table t_ywsl add aa varchar2(200);update t_ywsl set a ...
- OSX安装nginx和rtmp模块(rtmp直播服务器搭建)
1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- angular.js 字符串1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- C++序列化库的实现
C++中经常需要用到序列化与反序列化功能,由于C++标准中没有提供此功能,于是就出现了各式各样的序列化库,如boost中的,如谷歌的开源项目,但是很多库都依赖其他库过于严重,导致库变得很庞大.今天来分 ...
- 简单实现tab标签页切换
常见面试题: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- php之文件上传类代码
/* 单个文件上传 功能 上传文件 配置允许的后缀 配置允许的大小 获取文件后缀 判断文件的后缀 报错 */ class UpTool{ protected $allowExt = 'jpg,jpeg ...
- ubuntu更新源
源一定要找对应的版本 14.04对应 trusty deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multive ...
- Python Tutorial 学习(七)--Input and Output
7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...
- python django 自定义 装饰器
# -*-coding:utf-8-*- __author__ = "GILANG (pleasurelong@foxmail.com)" """ d ...