代码随想三刷二叉树篇1

2024-06-17 01:04
文章标签 代码 二叉树 随想 三刷

本文主要是介绍代码随想三刷二叉树篇1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码随想三刷二叉树篇1

  • 144. 二叉树的前序遍历
    • 题目
    • 代码
  • 145. 二叉树的后序遍历
    • 题目
    • 代码
  • 94. 二叉树的中序遍历
    • 题目
    • 代码
  • 102. 二叉树的层序遍历
    • 题目
    • 代码
  • 107. 二叉树的层序遍历 II
    • 题目
    • 代码
  • 199. 二叉树的右视图
    • 题目
    • 代码
  • 637. 二叉树的层平均值
    • 题目
    • 代码
  • 515. 在每个树行中找最大值
    • 题目
    • 代码
  • 104. 二叉树的最大深度
    • 题目
    • 代码
  • 111. 二叉树的最小深度
    • 题目
    • 代码
  • 226. 翻转二叉树
    • 题目
    • 代码

144. 二叉树的前序遍历

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list = new ArrayList();pre(root,list);return list;}public void pre(TreeNode root,List<Integer> list){if(root==null){return;}list.add(root.val);pre(root.left,list);pre(root.right,list);}
}

145. 二叉树的后序遍历

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> list = new ArrayList();pos(root,list);return list;}public void pos(TreeNode root,List<Integer> list){if(root==null){return;}pos(root.left,list);pos(root.right,list);list.add(root.val);}
}

94. 二叉树的中序遍历

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList();pos(root,result);return result;}public void pos(TreeNode root,List<Integer> list){if(root==null){return;}pos(root.left,list);list.add(root.val);pos(root.right,list);}
}

102. 二叉树的层序遍历

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {Deque<TreeNode> queue = new LinkedList();List<List<Integer>> result = new ArrayList();if(root==null){return result;}queue.addLast(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new ArrayList();for(int i = 0;i<size;i++){TreeNode temp = queue.removeFirst();list.add(temp.val);if(temp.left!=null){queue.addLast(temp.left);}if(temp.right!=null){queue.addLast(temp.right);}}result.add(list);}return result;}
}

107. 二叉树的层序遍历 II

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> result = new ArrayList();Deque<TreeNode> queue = new LinkedList();if(root==null){return result;}queue.addLast(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new ArrayList();for(int i=0;i<size;i++){TreeNode t = queue.removeFirst();list.add(t.val);if(t.left!=null){queue.addLast(t.left);}if(t.right!=null){queue.addLast(t.right);}}result.add(0,list);}return result;}
}

199. 二叉树的右视图

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> result = new ArrayList();if(root==null){return result;}Deque<TreeNode> queue = new LinkedList();queue.addLast(root);while(!queue.isEmpty()){int size = queue.size();for(int i=0;i<size;i++){TreeNode t = queue.removeFirst();if(t.left!=null){queue.addLast(t.left);}if(t.right!=null){queue.addLast(t.right);}if(i==size-1){result.add(t.val);}}}return result;}
}

637. 二叉树的层平均值

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> result = new ArrayList();if(root==null){return result;}Deque<TreeNode> queue = new LinkedList();queue.addLast(root);while(!queue.isEmpty()){int size = queue.size();double sum = 0;for(int i=0;i<size;i++){TreeNode t = queue.removeFirst();sum += t.val;if(t.left!=null){queue.addLast(t.left);}if(t.right!=null){queue.addLast(t.right);}}result.add(sum/size);}return result;}
}

515. 在每个树行中找最大值

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> result = new ArrayList();if(root==null){return result;}Deque<TreeNode> queue = new LinkedList();queue.addLast(root);while(!queue.isEmpty()){int size = queue.size();int num = Integer.MIN_VALUE;for(int i=0;i<size;i++){TreeNode t = queue.removeFirst();num = Math.max(num,t.val);if(t.left!=null){queue.addLast(t.left);}if(t.right!=null){queue.addLast(t.right);}}result.add(num);}return result;}
}

104. 二叉树的最大深度

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public int maxDepth(TreeNode root) {findDepth(root,1);return maxDepth;}int maxDepth = 0;public void findDepth(TreeNode root,int depth){if(root==null){return;}maxDepth = Math.max(maxDepth,depth);findDepth(root.left,depth+1);findDepth(root.right,depth+1);}
}

111. 二叉树的最小深度

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public int minDepth(TreeNode root) {if(root==null){return 0;}pre(root,1);return min;}   int min = Integer.MAX_VALUE;public void pre(TreeNode root,int depth){if(root==null){return;}if(root.left==null&&root.right==null){min = Math.min(min,depth);}pre(root.left,depth+1);pre(root.right,depth+1);}
}

226. 翻转二叉树

题目

链接

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode invertTree(TreeNode root) {if(root==null||(root.left==null&&root.right==null)){return root;}TreeNode left= invertTree(root.left);TreeNode right= invertTree(root.right);root.left = right;root.right = left;return root;}
}

这篇关于代码随想三刷二叉树篇1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068055

相关文章

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La