Java二叉搜索树与数组查找的方法
作者:归人云淡风轻
时间:2023-04-25
浏览: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
作者最新文章
LibreOffice无法更新到最新版本怎么解决
2026-11-24 13:44
蛙漫2正版登录入口及漫画更新指南
2026-11-23 10:44
包子漫画官网入口网页版
2026-11-23 10:33
红果短剧电脑登录入口 网页版免费看
2026-11-23 10:24
漫蛙manwa防丢网站2026最新地址
2026-11-22 16:55
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































