代码随想三刷二叉树篇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中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部