LeetCode 数、二叉树、二叉搜索树篇(94、144、145)
94. 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
//中序遍历 必会
//两个思路 递归、栈
//第三种方法 颜色标记法 来及网友Henry
//核心思想,搞一个栈用来存储遍历过的,根据要求遍历的顺序,将可以放入的指为灰色,不能的为白色
//不断弹出栈顶元素进行判断
//本质为自己手动维护一个递归的栈
//第四种方法 莫里斯中序遍历 通过改变树的结构的遍历(不做要求)
solution1 递归
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List <Integer> res = new ArrayList <> ();
helper(root, res);
return res;
}
private void helper(TreeNode root, List <Integer> res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
}
solution2 栈
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List <Integer> res = new ArrayList <> ();
Stack <TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()){
//先把根结点放入stack
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}
}
solution3 颜色标记法
class Solution {
class ColorNode{
TreeNode node;
String color;
public ColorNode(TreeNode node,String color){
this.node = node;
this.color = color;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) return new ArrayList<Integer>();
List<Integer> res = new ArrayList<>();
Stack<ColorNode> st = new Stack<>();
st.push(new ColorNode(root,"white"));
while (!st.isEmpty()){
ColorNode cn = st.pop(); //先把结点pop出来
if (cn.color == "white"){
if (cn.node.right != null) st.push(new ColorNode(cn.node.right,"white"));
st.push(new ColorNode(cn.node,"grey"));
if (cn.node.left != null) st.push(new ColorNode(cn.node.left,"white"));
}else {
res.add(cn.node.val);
}
}
return res;
}
}
144. 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
solution1 递归
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(root,res);
return res;
}
private void helper(TreeNode root,List<Integer> res){
if (root == null) return;
res.add(root.val);
helper(root.left, res);
helper(root.right, res);
}
}
solution2 用栈进行迭代
//用栈保存根结点,最后一棵树放在最上方
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> st = new Stack<>();
TreeNode curr = root;
while (curr != null || !st.isEmpty()){ //防止一开始为空
while (curr != null) {
res.add(curr.val);
st.push(curr);
curr = curr.left;
}
curr = st.pop().right;
}
return res;
}
}
solution3 颜色标记法
class Solution {
class ColorNode{
TreeNode node;
String color;
public ColorNode(TreeNode node,String color){
this.node = node;
this.color = color;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<ColorNode> st = new Stack<>();
st.push(new ColorNode(root,"white"));
while (!st.isEmpty()){
ColorNode cn = st.pop();
if (cn.color == "white"){
if (cn.node.right != null) st.push(new ColorNode(cn.node.right,"white"));
if (cn.node.left != null) st.push(new ColorNode(cn.node.left,"white"));
st.push(new ColorNode(cn.node,"grey"));
}else {
res.add(cn.node.val);
}
}
return res;
}
}
145. 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
//三种思路
//新递归(终结条件+当前层+下探层+(清理本层))
//迭代 (把左树和根压入栈,再从栈弹出,判断是否有右子树)
//颜色标记
solution1 递归
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(root,res);
return res;
}
private void helper(TreeNode root,List<Integer> list){
if(root==null) return;
helper(root.left,list);
helper(root.right,list);
list.add(root.val);
}
}
solution2 栈+迭代
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<TreeNode> st = new Stack<>();
TreeNode curr = root;
TreeNode last = null;//用于遍历右子树时,存储上一个节点
while(curr!=null || !st.isEmpty()){
//压根和左子树
while(curr!=null){
st.push(curr);
curr = curr.left;
}
curr = st.peek(); //判断是否有右数
if(curr.right == null || curr.right == last){ //右边为空,或者已经遍历过了
res.add(st.pop().val);
last = curr;//记录
curr = null;
}else{
curr = curr.right;
}
}
return res;
}
}
solution3 颜色标记法
class Solution {
class ColorNode{
TreeNode node;
String color;
public ColorNode(TreeNode root,String color){
this.node = root;
this.color = color;
}
}
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<ColorNode> st = new Stack<>();
st.push(new ColorNode(root,"white"));
while(!st.isEmpty()){
ColorNode cn = st.pop();
if(cn.color == "white"){
st.push(new ColorNode(cn.node,"grey"));
if (cn.node.right!= null) st.push(new ColorNode(cn.node.right,"white"));
if (cn.node.left!= null) st.push(new ColorNode(cn.node.left,"white"));
}else{
res.add(cn.node.val);
}
}
return res;
}
}
LeetCode 数、二叉树、二叉搜索树篇(94、144、145)的更多相关文章
- LeetCode:验证二叉搜索树【98】
LeetCode:验证二叉搜索树[98] 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当 ...
- C#LeetCode刷题-二叉搜索树
二叉搜索树篇 # 题名 刷题 通过率 难度 220 存在重复元素 III 19.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 327 区间和的个数 29.5% 困难 3 ...
- Leetcode:235. 二叉搜索树的最近公共祖先
Leetcode:235. 二叉搜索树的最近公共祖先 Leetcode:235. 二叉搜索树的最近公共祖先 Talk is cheap . Show me the code . /** * Defin ...
- Leetcode:530. 二叉搜索树的最小绝对差
Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...
- 代码随想录算法训练营day23 | leetcode 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树
LeetCode 669. 修剪二叉搜索树 分析1.0 递归遍历树时删除符合条件(不在区间中)的节点-如何遍历如何删除 如果当前节点大于范围,递归左树,反之右树 当前节点不在范围内,删除它,把它的子树 ...
- 树(二叉树 & 二叉搜索树 & 哈夫曼树 & 字典树)
树:n(n>=0)个节点的有限集.有且只有一个root,子树的个数没有限制但互不相交.结点拥有的子树个数就是该结点的度(Degree).度为0的是叶结点,除根结点和叶结点,其他的是内部结点.结点 ...
- LeetCode 98 验证二叉搜索树
题目: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是 ...
- Leetcode 98 验证二叉搜索树 Python实现
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)
题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也 ...
- 【LeetCode】不同二叉搜索树
[问题] 卡特兰(Catalan)数来源于卡特兰解决凸n+2边形的剖分时得到的数列Cn,在数学竞赛.信息学竞赛.组合数学.计算机编程等方面都会有其不同侧面的介绍.卡特兰问题的解决过程应用了大量的映射方 ...
随机推荐
- 前端CSS五中元素定位类型
元素想通过底部.顶部.左侧.右侧属性定位是必须先设定position的属性值 posistion属性的五个值:static.relative.fixed.absoulte.sticky static定 ...
- 深入理解 Netty FastThreadLocal
作者:vivo 互联网服务器团队- Jiang Zhu 本文以线上诡异问题为切入点,通过对比JDK ThreadLocal和Netty FastThreadLocal实现逻辑以及优缺点,并深入解读源码 ...
- node(1)
1.新建http.js //node搭建http服务器 let http=require('http'); //使用http建立服务请求 http.createServer(function(requ ...
- Ansible与Ansible部署
Ansible与Ansible部署 Ansible简介: Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩.它融合了众多老牌运维工具的优点,Pubbet ...
- ACAM 学习笔记 | 附 YbtOJ 全部题解
怎么有人现在才学 ACAM 呢. 好像比 SAM 简单挺多啊,也不记得当时是哪里看不懂. AC 自动机() 自动 AC 机(✘) 概述 ACAM(Aho–Corasick Automaton),是用来 ...
- Velocity之Hello World(入门实例)
参考:http://blog.csdn.net/mengxuwq/article/details/1871161 (非常感谢这篇文章,让我初步入门) 自己调试完全能运行后,写在此,供新人参考,供自己温 ...
- DM数据库SQL分页案例
DM一哥们找我优化条分页的SQL语句,结果集很小返回99行数据,废话不说安排一下. 原始SQL语句如下,保密要求,给真实的表名换了别名: SELECT count(*) FROM (SELECT TM ...
- .Net8 Blazor 尝鲜
全栈 Web UI 随着 .NET 8 的发布,Blazor 已成为全堆栈 Web UI 框架,可用于开发在组件或页面级别呈现内容的应用,其中包含: 用于生成静态 HTML 的静态服务器呈现. 使用 ...
- 记录一次 postgresql 优化案例( 嵌套循环改HASH JOIN )
今天同事给我一条5秒的SQL看看能不能优化. 表数据量: select count(1) from AAAA union all select count(1) from XXXXX; count - ...
- DataGridView合并单元格,重绘后选中内容被覆盖的解决办法
DataGridView合并单元格只能进行重绘,网上基本上使用的是下面的方法: 1 /// <summary> 2 /// 说明:纵向合并单元格 3 /// 参数:dgv DataGrid ...