Java binary search Tree and Array search method
This article introduces the relevant knowledge of "Java binary search tree and array search method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Topic one
Solution method
/ * 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);}} topic 2
Solution method
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 (2s)