【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 ...
随机推荐
- SQL 自定义四舍五入
--============================================== -- 自定义的四舍五入(四舍五入后的所有尾数遇进则进) -- by 小天使 2015-11-12 -- ...
- UVA 509 RAID!
思路来自:https://blog.csdn.net/wcr1996/article/details/43834545 先解释一下题意: ①输入:先输入d(disk的数量) s(每块数据块有s个bi ...
- 1046 Gridland
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1046 难点在于读懂题意 题意:输入一个n*m的点阵,间距为1,问你遍历完所有点阵并回到起点的最短路径是多少 ...
- JAVA的8种基本数据类型和类型转换
byte 字节型 1字节(8bit) 初始值:0 char 字符型 2字节 空格 short 短整型 2字节 0 int 整形 4字节 0 long ...
- Python2.*与Python3.*共存问题
安装Python 2.7后,本来在3.4下能正常使用的脚本无法运行.网上有的方法是把两个版本的主程序分别改名为python2和python3,人眼判断脚本,手输命令行执行脚本.像我这样喜欢双击.拖拽的 ...
- LINUX文件内容处理及文本编辑器vim
Linux基本操作命令 echo命令 echo命令: 把echo后面的内容显示到屏幕. -n 显示内容的时候不显示每行结尾的回车 echo 内容 >> 文件名 表示把内容加到一个文件的末尾 ...
- 2018年东北农业大学春季校赛 E-wyh的阶乘(求n!的0的个数)
链接:https://www.nowcoder.com/acm/contest/93/E来源:牛客网 题目描述 这个问题很简单,就是问你n的阶乘末尾有几个0? 输入描述: 输入第一行一个整数T(1&l ...
- pygame-KidsCanCode系列jumpy-part7-游戏启动/结束画面
通常一个游戏启动(start)或结束(game over)时,都会显示一个画面,来引导用户.这节,我们学习如何处理这块逻辑. 其实之前,我们已经预留了2个函数,只要把它实现即可: def show_s ...
- 浏览器模式&用户代理字符串(IE)
问题问题描述今天在做项目的时候,QA部门提了一个Bug,在一个搜索列表中,搜索栏为空时刷新页面,却触发了搜索功能,并且列表显示出<未搜索到结果> 环境IE11 问题原因 QA的IE11用户 ...
- Deepin 15.4 挂载分区
硬盘是 500G,先前安装 Windows7 时,分了3个区,大小分别为 90G.100G.310G,现在用 Deepin 重装系统时,挂载情况如下: /dev/sda1 分区作为 /(挂载点),类型 ...