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)的更多相关文章

  1. LeetCode:验证二叉搜索树【98】

    LeetCode:验证二叉搜索树[98] 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当 ...

  2. C#LeetCode刷题-二叉搜索树

    二叉搜索树篇 # 题名 刷题 通过率 难度 220 存在重复元素 III   19.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 327 区间和的个数   29.5% 困难 3 ...

  3. Leetcode:235. 二叉搜索树的最近公共祖先

    Leetcode:235. 二叉搜索树的最近公共祖先 Leetcode:235. 二叉搜索树的最近公共祖先 Talk is cheap . Show me the code . /** * Defin ...

  4. Leetcode:530. 二叉搜索树的最小绝对差

    Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...

  5. 代码随想录算法训练营day23 | leetcode 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树

    LeetCode 669. 修剪二叉搜索树 分析1.0 递归遍历树时删除符合条件(不在区间中)的节点-如何遍历如何删除 如果当前节点大于范围,递归左树,反之右树 当前节点不在范围内,删除它,把它的子树 ...

  6. 树(二叉树 & 二叉搜索树 & 哈夫曼树 & 字典树)

    树:n(n>=0)个节点的有限集.有且只有一个root,子树的个数没有限制但互不相交.结点拥有的子树个数就是该结点的度(Degree).度为0的是叶结点,除根结点和叶结点,其他的是内部结点.结点 ...

  7. LeetCode 98 验证二叉搜索树

    题目: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是 ...

  8. Leetcode 98 验证二叉搜索树 Python实现

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

  9. LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)

    题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也 ...

  10. 【LeetCode】不同二叉搜索树

    [问题] 卡特兰(Catalan)数来源于卡特兰解决凸n+2边形的剖分时得到的数列Cn,在数学竞赛.信息学竞赛.组合数学.计算机编程等方面都会有其不同侧面的介绍.卡特兰问题的解决过程应用了大量的映射方 ...

随机推荐

  1. 什么是 CSS?

    1.什么是 CSS? CSS 指的是层叠样式表* (Cascading Style Sheets) CSS 描述了如何在屏幕.纸张或其他媒体上显示 HTML 元素 CSS 节省了大量工作.它可以同时控 ...

  2. Chapter 57. Multi-project Builds

    http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects The po ...

  3. My Code Style

    大家都在写,跟风. 头文件 万能头.因为我刚学 OI 的时候怎么都背不住 algorithm 怎么拼( 变量 数组开全局,一些前后重名/只在某一部分用的变量开局部. 尽量不使用指针/ stl 迭代器等 ...

  4. 手撕Vuex-实现mutations方法

    经过上一篇章介绍,完成了实现 getters 的功能,那么接下来本篇将会实现 mutations 的功能. 在实现之前我们先来回顾一下 mutations 的使用. 将官方的 Vuex 导入进来,因为 ...

  5. DS必背合集

    Data Structure必背合集 一.链表.栈和队列 1.简述说明数据的存储结构: 答: (1)顺序存储:逻辑上相邻的两个元素的物理位置也相邻. 优点:能够随机存取. 缺点:插入删除需要移动大量的 ...

  6. CSS 溢出overflow属性的使用

    作者:WangMin 格言:努力做好自己喜欢的每一件事 在CSS中,如果给一个盒子设置了固定的宽度与高度,但内容过多就会溢出盒子本身的宽度或高度.此时,就可以使用 overflow 属性来控制内容溢出 ...

  7. Welcome to YARP - 4.限流 (Rate Limiting)

    目录 Welcome to YARP - 1.认识YARP并搭建反向代理服务 Welcome to YARP - 2.配置功能 2.1 - 配置文件(Configuration Files) 2.2 ...

  8. 使用Python将MySQL查询结果导出到Excel(xlsxwriter)

    在实际工作中,我们经常需要将数据库中的数据导出到Excel表格中进行进一步的分析和处理.Python中的pymysql和xlsxwriter库提供了很好的解决方案,使得这一过程变得简单而高效. 建立数 ...

  9. KMeans算法全面解析与应用案例

    本文深入探讨了KMeans聚类算法的核心原理.实际应用.优缺点以及在文本聚类中的特殊用途,为您在聚类分析和自然语言处理方面提供有价值的见解和指导. 关注TechLead,分享AI全维度知识.作者拥有1 ...

  10. 使用 Hexo 搭建个人博客并部署到云服务器

    目录 1 整体流程 2. 本地环境准备 2.1 安装 Node.js 和 Git 2.2 安装 Hexo 3. 服务端环境准备 3.1 Nginx 环境配置 3.1.1 安装 Nginx 3.1.2 ...