Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
Input: root = [4,2,6,1,3]
Output: 1
Example 2:
Input: root = [1,0,48,null,null,12,49]
Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 104].
0 <= Node.val <= 105
Original Page
*Wrong Code for the first attempt
public int getMinimumDifference(TreeNode root) { int min = Integer.MAX_VALUE; Deque<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()){ TreeNode cur = queue.poll(); if(cur !=null){ if(cur.left!=null){ min = Math.min(min, Math.abs(cur.val - cur.left.val)); } if(cur.right !=null){ min = Math.min(min, Math.abs(cur.val - cur.right.val)); } queue.offer(cur.left); queue.offer(cur.right); } } return min; }
The Above code cannot solve the situation when two adjacent elements are not a direct parent-child relationship.
public int getMinimumDifference(TreeNode root) { int min = Integer.MAX_VALUE; return getMin(root,null, min); } public int getMin(TreeNode cur,TreeNode pre, int min){ if(cur == null){ return min; } min = getMin(cur.left,pre, min); if(pre!=null){ min = Math.min(min, Math.abs(pre.val-cur.val)); } pre = cur; min = getMin(cur.right,pre, min); return min; }
TreeNode pre; public int getMinimumDifference(TreeNode root) { int min = Integer.MAX_VALUE; return getMin(root, min); } public int getMin(TreeNode cur, int min){ if(cur == null){ return min; } min = getMin(cur.left, min); if(pre!=null){ min = Math.min(min, Math.abs(pre.val-cur.val)); } min = getMin(cur.right, min); return min; }
it will cause some problem that the 1st element is not the first element the pre will be the first, so it will cause trouble here.
e.g.
so here it may pre == null will lead to first logic
Here we can also use two value conduct this problem
public int getMinimumDifference(TreeNode root) { int[] min = {Integer.MAX_VALUE}; getMin(root,null, min); return min[0]; } public TreeNode getMin(TreeNode cur,TreeNode pre, int[] min){ if(cur == null){ return pre; } pre = getMin(cur.left,pre, min); if(pre!=null){ min[0] = Math.min(min[0], Math.abs(pre.val-cur.val)); } pre = cur; pre = getMin(cur.right,pre, min); return pre; }
Be careful Java use Value Pass so it will not work when we pass by using int or others so that we can pass an object it will re-assign element of the array so that will work
then be careful we pass pre instead of cur, because we want the first pre is start from the left node (if we pass cur it will cause when we start mid-logic, the pre is still not in the right place it will be 1 element earlier than cur, it is not resealable)
Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
If the tree has more than one mode, return them in any order.
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Both the left and right subtrees must also be binary search trees.
Example 1:
Input: root = [1,null,2,2]
Output: [2]
Example 2:
Input: root = [0]
Output: [0]
Constraints:
The number of nodes in the tree is in the range [1, 104].
-105 <= Node.val <= 105
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
lol, Practicing Stream is so cool but not efficient, may it work well in real-practice env?
public int[] findMode(TreeNode root) { Map<Integer,Integer> map = new HashMap<>(); map = modeLogic(root,map); int max = Collections.max(map.values()); List<Integer> list = new ArrayList<>(); list = map.entrySet().stream() .filter(entry -> entry.getValue().equals(max)) .map(Map.Entry::getKey) .collect(Collectors.toList()); return list.stream() .mapToInt(Integer::valueOf) .toArray(); } public Map<Integer,Integer> modeLogic(TreeNode cur, Map<Integer,Integer> map){ if(cur==null){ return map; } //left modeLogic(cur.left, map); //mid map.put(cur.val, map.getOrDefault(cur.val, 0)+1); //right modeLogic(cur.right, map); return map; }
Methode 2. Because it is BST, so it will be easier when we use in-order transverse.