当前位置:网站首页>二叉树 6/21 91-95
二叉树 6/21 91-95
2022-08-10 05:36:00 【吉良吉影__.】
1.找树左下角的值( LeetCode 513 )
层序遍历
/** * 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 findBottomLeftValue(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.addLast(root);
int ans = 0;
while (!deque.isEmpty()){
int num = deque.size();
for (int i = 0; i < num; i++) {
TreeNode temp = deque.removeFirst();
if (i == 0)ans = temp.val;
if (temp.left != null)deque.addLast(temp.left);
if (temp.right != null)deque.addLast(temp.right);
}
}
return ans;
}
}
2.修剪二叉搜索树( LeetCode 669 )
递归
利用二叉搜索树的性质
/** * 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 trimBST(TreeNode root, int low, int high) {
if (root == null)return root;
root.left = trimBST(root.left,low,high);//左节点置为处理好的节点
root.right = trimBST(root.right,low,high);//右节点置为处理好的节点
if (root.val < low){
//情况一
//root节点及左侧节点全部丢弃,因为root是二叉搜索树
return root.right;
}
if (root.val > high){
//情况二
return root.left;
}
return root;
}
}
3.二叉搜索树的最近公共祖先( LeetCode 235 )
技巧
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
TreeNode ans;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
dfs(root,p,q);
return ans;
}
public boolean dfs(TreeNode node,TreeNode p,TreeNode q){
if (node == null)return false;
boolean lb = dfs(node.left,p,q);
boolean rb = dfs(node.right,p,q);
if ((lb && rb) || ((lb || rb) && (p == node || q == node))){
ans = node;
}
return lb || rb || p == node || q == node;
}
}
4.二叉搜索树的最小绝对差( LeetCode 530 )
中序遍历
/** * 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 {
List<Integer> list = new ArrayList<>();
public int getMinimumDifference(TreeNode root) {
dfs(root);
int ans = Integer.MAX_VALUE;
for (int i = 1; i < list.size(); i++) {
ans = Math.min(ans,list.get(i) - list.get(i - 1));
}
return ans;
}
public void dfs(TreeNode node){
if (node == null)return;
dfs(node.left);
list.add(node.val);
dfs(node.right);
}
}
5.最大二叉树( LeetCode 654 )
递归
/** * 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 constructMaximumBinaryTree(int[] nums) {
return build(nums,0,nums.length - 1);
}
public TreeNode build(int[] nums,int l,int r){
if (l > r)return null;
int max = nums[l];
int maxidx = l;
for (int i = l + 1; i <= r; i++) {
if (nums[i] > max){
max = nums[i];
maxidx = i;
}
}
TreeNode node = new TreeNode(max);
node.left = build(nums,l,maxidx - 1);
node.right = build(nums,maxidx + 1,r);
return node;
}
}
边栏推荐
猜你喜欢

Radon 变换原理和应用

ASP.Net利用代码点击相应按钮来关闭当前的页面(亲测有效)

51单片机ST188手持人体温度脉搏心率测量仪锂电池充电

【从零设计 LaTex 模板】1. 一些基础知识

Deep learning TensorFlow entry environment configuration

pytorch-10. Convolutional Neural Networks

pytorch-10. Convolutional Neural Networks (homework)

STC12C5A60S2单片机WIFI信号扫描报警监视系统信号增强信号过低报警

以STM32F103C6T6为例通过配置CubeMX实现EXIT外部中断

STM32F407ZG GPIO输入相关实验
随机推荐
开源游戏服务器框架NoahGameFrame(NF)简介(一)
clickhouse出现数据重复问题
51单片机AD590温度测量ADC0832运放2.73V减法电压变换
51单片机室内环境甲醛PM2.5光照温度湿度检测及窗帘加湿消毒控制系统
每日刷题(day03)——leetcode 899. 有序队列
GC0053-STM32单片机NTC热敏电阻温度采集及控制LCD1602
STC12C5A60S2单片机WIFI信号扫描报警监视系统信号增强信号过低报警
C#对MySQL数据库进行增删改查操作(该操作还有防止MySQL注入功能)
【接口自动化】
酸回收工艺原理
常用模块封装-csv文件操作封装
探究乱码问题的本源:GBK,UTF8,UTF16,UTF8BOM,ASN1之间的关联
酸回收树脂工艺技术详解
Unity热更新哪些事
【简易笔记】PyTorch官方教程简易笔记 EP4
享元模式-缓存池
浅谈游戏中3种常用阴影渲染技术(1):平面阴影
如何实现网格建造系统
在Unity中让物体围绕自身的x、y、z轴进行旋转(亲测有效)
Multisim软件的基本使用