Java二叉搜索树与数组查找的方法
作者:归人云淡风轻
时间:2023-04-24
来源:互联网
浏览:0
题目一解法/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNodeleft,TreeNoderight){*this.val=val;*this.left=left;*this.right=right;*}*}*/classSolution{i
题目一

解法
/**
* 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 {
int ans;
int pre;
public int minDiffInBST(TreeNode root) {
ans = Integer.MAX_VALUE;
pre = -1;
method(root);
return ans;
}
public void method(TreeNode root){
if(root==null) return;
method(root.left);
if(pre==-1){
pre = root.val;
}else{
ans = Math.min(ans,root.val-pre);
pre = root.val;
}
method(root.right);
}
}题目二

解法
class Solution {
public int dominantIndex(int[] nums) {
int f = Integer.MIN_VALUE;
int fi = 0;
int s = Integer.MIN_VALUE;
int si = 0;
for(int i = 0; if){
s = f;
f = nums[i];
fi = i;
}else if(nums[i]>s){
s = nums[i];
}
}
if(nums.length==1) return 0;
if(2*s<=f) return fi;
return -1;
}
} 题目三

解法
class Solution {
public int repeatedNTimes(int[] nums) {
int n = nums.length/2;
HashMap map =new HashMap();
for(int key : nums){
if(map.containsKey(key)){
map.put(key,map.get(key)+1);
if(map.get(key)==n){
return key;
}
}else{
map.put(key,1);
}
}
return 0;
}
} 题目四

解法
class Solution {
public boolean uniqueOccurrences(int[] arr) {
int[] nums = new int[2000];
for(int i =0;i set =new HashSet();
for(int i =0;i
作者最新文章
荣耀MagicOS 11发布计划与Agent Harness架构解析
2026-09-08 19:23
AI重构企业业务架构:超聚变“智企”范式核心解析
2026-09-08 18:39
PDF合并工具怎么选?在线合并5步实操指南
2026-09-04 17:05
PDF图片压缩工具推荐与批量处理实操指南
2026-09-03 12:14
照片如何转成PDF格式?三种图片转PDF操作方法
2026-09-03 11:04
上一篇:
java虚拟扩展方法怎么实现
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















