【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法
前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
list.add(p.val); // Add before going to children
p=p.left;
}else{
TreeNode node=stack.pop();
p=node.right;
}
}
return list;
}
中序遍历
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
p=p.left;
}else{
TreeNode node=stack.pop();
list.add(node.val);// Add after all left children
p=node.right;
}
}
return list;
}
后序遍历
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> list = new LinkedList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
list.addFirst(p.val);// Reverse the process of preorder
p=p.right;
}else{
TreeNode node=stack.pop();
p=node.left;
}
}
return list;
}
【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法的更多相关文章
- 剑指 Offer 36. 二叉搜索树与双向链表 + 中序遍历 + 二叉排序树
剑指 Offer 36. 二叉搜索树与双向链表 Offer_36 题目描述 题解分析 本题考查的是二叉树的中序遍历以及二叉排序树的特征(二叉排序树的中序遍历序列是升序序列) 利用排序二叉树中序遍历的性 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)
题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...
- 剑指offer-面试题36-二叉搜索树与双向链表-中序遍历
/* 题目: 将二叉搜索树转化为排序的双向链表,不能创建新的节点, 只能调整节点的指向,返回双向链表的头节点. */ /* 思路: 递归. 二叉搜索树的中序遍历得到的序列是递增序列. 左子树left& ...
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...
- [Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
随机推荐
- SpringBoot拦截器
在实际开发中,总存在着这样的场景,比如拦截请求的ip地址,或者在所有的请求都返回相同的数据,如果每一个方法都写出相同数据固然可以实现,但是随着项目的变大,重复的代码会越来越多,所以在这种情况我们可以用 ...
- JS 对象引用问题
var a = {n:1}; var b = a; a = {n:2}; a.x = a ;console.log(a.x);console.log(b.x); var a = {n:1}; var ...
- redis(三)
string string是redis最基本的类型 最大能存储512MB数据 string类型是二进制安全的,即可以为任何数据,比如数字.图片.序列化对象等 命令 设置 设置键值 set key va ...
- Coins [POJ1742] [DP]
Description 给出硬币面额及每种硬币的个数,求从1到m能凑出面额的个数. Input 多组数据,每组数据前两个数字为n,m.n表示硬币种类数,m为最大面额,之后前n个数为每种硬币的面额, ...
- 检测版本更新,iOS
检测版本更新的方法. //检查新版本 更新 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^ ...
- 3_主流部署方式介绍-Django+mod_wsgi+Apache
安装apache yum install httpd httpd-devel -y 开机自动重启配置 chkconfig httpd on 重新编译安装python 删除编译记录及文件 修改apach ...
- python之函数第二篇
一.名称空间与作用域 名称空间分类: 内置名称空间 import this dir(buil-in) 查看全部内置 全局名称空间 局部名称空间 在函数体内等 查询全局和局部 globals()方法可以 ...
- CSS学习之路,指定值,计算值,使用值。
前面被问过这几个值得区别,没太研究,有点抠文字的感觉,既然到这儿了 ,就简答梳理下吧. 指定值(specified value):通过样式表样式规则定义的值:可以来自层叠样式表,如果没有指定,则考虑父 ...
- .Net转Java.08.format
%[index$][标识][最小宽度]转换方式 [index$]可以用于表示对第index个参数进行格式化, // Java代码 String s1=String.format("%3$s, ...
- centos7下使用rpm包安装clickhouse
clickhouse是由俄罗斯Yandex公司开发的列式存储数据库,于2016年开源,clickhouse的定位是快速的数据分析,对于处理海量数据的情况性能非常好,在网上也有很多测试的案例,在大数据的 ...