Binary Tree Non-recursive Traversal
Preorder:
因为是preorder traversal, 我们需要先print root,然后左节点,最后右节点,而且root左边子树一定比右边子树先print出来,所以,我们可以先把当前root的右节点压栈,然后把root的左节点压栈,这样每次从栈里取的时候,可以保证左边节点的root先取。同时,每次取了当前节点,我们进行同样的操作(先压右节点,再压左节点),这样可以保证preorder traversal。
/**
* 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) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> s = new Stack<TreeNode>();
s.push(root);
while (!s.empty()) {
TreeNode node = s.pop();
list.add(node.val);
if (node.right != null) {s.push(node.right);}
if (node.left != null) {s.push(node.left);}
}
return list;
}
}
Inorder:
因为inorder 需要先打印最左边,然后root,然后最右边,所以,我们一定要先reach到树的最左边,直到没有左子树为止,并同时把root加入到stack里。
当当前node没有左子树,表面我们已经到达树的最左边,我们需要把stack最上面的root打出来,然后当前root指向root.right. 然后把右子树当成一颗树使用同样的遍历即可。这题的关键点之一是那个while条件,也就是说,只要stack不为空或者当前node不是null, 我们就应该继续。
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}
}
Postorder:
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new LinkedList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
root = stack.pop();
list.add(, root.val);
if (root.left != null) { stack.push(root.left); }
if (root.right != null) { stack.push(root.right); }
}
return list;
}
}
Binary Tree Non-recursive Traversal的更多相关文章
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
随机推荐
- 安装 SQL server 2008 R2
操作系统:WIN7 问题: The Windows Installer Service could not be accessed. This can occur if the Windows Ins ...
- WebGrid Helper with Check All Checkboxes
WebGrid Helper with Check All Checkboxes myEvernote Link Tuesday, September 13, 2011ASP.NET ASP.NET ...
- 浅谈T-SQL中的联接查询
引言 平时开发时,经常会使用数据库进行增删改查,免不了会涉及多表联接.今天就简单的记录下T-SQL下的联接操作. 联接类型及其介绍 在T-SQL中联接操作使用的是JOIN表运算符.联接有三种基本的类型 ...
- java 递归获取一个目录下的所有文件路径
还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...
- [Mongodb]删除指定字段
摘要 如果想要删除mongodb中一个document的某个字段,该如何做呢? 方法模版 db.user.update({"email_state":{"$exists& ...
- [工具]json转类
摘要 这周在园子看到一篇介绍JsonCSharpClassGenerator这个工具的文章,感觉挺实用的,在现在项目中json用的是最多的,所以在转换对应的类的时候,确实挺频繁,所以就研究了一下这个工 ...
- Promise 异步(asynchronous )编程
概述 Promise.all(iterable) 方法返回一个promise,该promise会等iterable参数内的所有promise都被resolve后被resolve,或以第一个promis ...
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- jQuery 学习之路(3):DOM操作
一.在元素内部插入 .append(content[,content]) 在指定元素的内部末尾插入字符串,或在指定元素的内部末尾移入指定元素,如 $(".container").a ...
- C++ Singleton模式
地址:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html Singleton模式是常用的设计模式之一,但是要实现一个真正实用的设计模式却 ...