代码随想三刷二叉树篇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

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT