代码随想录 第21天 | ● 530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先

leetcode:530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

思路:判断最小绝对差,肯定用中序遍历,双指针一前一后依次判断。

class Solution {
    int result = Integer.MAX_VALUE;
    TreeNode pre = null;

    public int getMinimumDifference(TreeNode root) {
        if(root == null) return  0;
        getmin(root);
        return  result;
    }

    private void getmin(TreeNode cur) {
        if(cur == null) return ;
        //中序
        getmin(cur.left);

        if(pre != null){
            result = Math.min(result,Math.abs(pre.val-cur.val));
        }
        pre = cur;
        getmin(cur.right);
    }
}

leetcode:501. 二叉搜索树中的众数 - 力扣(LeetCode)

思路:二叉搜索树,中序遍历,找众数,可能有多个,所以要找到最大出现次数,当MAx找到时,再一一和count比对,一样就放到数组里。还是用了双指针查找,前后cur相同++,不相同重新计数

class Solution {
    ArrayList<Integer> arrayList = new ArrayList<>();
    TreeNode pre = null;
    int Maxcount = 0;
    int count = 0;
    public int[] findMode(TreeNode root) {
        if( root == null) return  null;
        getMode(root);
        int [] res = new int[arrayList.size()];
        for(int i = 0 ;i < res.length; i++){
            res[i] = arrayList.get(i);
        }
        return res;
    }

    private void   getMode(TreeNode cur) {

        if( cur == null) return;
        getMode(cur.left);
        int val = cur.val;
        //计数
        if (pre == null || val != pre.val) {
            count = 1;
        } else {
            count++;
        }
        //更新max
        if(count > Maxcount){
            Maxcount = count;
            arrayList.clear();
            arrayList.add(cur.val);
        }else if (count == Maxcount){
            arrayList.add(cur.val);
        }
        pre = cur;
        getMode(cur.right);
        return;

    }
}

普通二叉数:随便遍历二叉数,将所有出现值放入map里,出现一次+1,最后遍历找出最高频率的数。

class Solution {
    public int[] findMode(TreeNode root) {
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        if (root == null) return list.stream().mapToInt(Integer::intValue).toArray();
        // 获得频率 Map
        searchBST(root, map);
        List<Map.Entry<Integer, Integer>> mapList = map.entrySet().stream()
                .sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue()))
                .collect(Collectors.toList());
        list.add(mapList.get(0).getKey());
        // 把频率最高的加入 list
        for (int i = 1; i < mapList.size(); i++) {
            if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) {
                list.add(mapList.get(i).getKey());
            } else {
                break;
            }
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    void searchBST(TreeNode curr, Map<Integer, Integer> map) {
        if (curr == null) return;
        map.put(curr.val, map.getOrDefault(curr.val, 0) + 1);
        searchBST(curr.left, map);
        searchBST(curr.right, map);
    }

}

leetcode:236. 二叉树的最近公共祖先 - 力扣(LeetCode)

思路:后序遍历,因为要看左右节点的情况才能做判断,判断节点三种情况,什么是递归回溯,就是能够一直到二叉数的最深处,这是递归,如果出现满足三种情况的任一一种,就返回根节点,这是回溯

1.

//两节点都不为空,说明pq都找到了。

2.

//左不为空,说明左孩子找到p或q了

3. 

//说明右孩子找到p或q了
class Solution {
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;
        //后序
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(root == p || root == q) return root;
        //两节点都不为空,说明pq都找到了。
        if(left != null && right != null){
            return  root;
        }else if(left != null && right == null) return left;//说明左孩子找到p或q了
        else if(left == null && right != null) return right;//说明右孩子找到p或q了
        else return  null; //都没找到

    }
}

 

posted @ 2024-03-15 21:46  22软工冷薄  阅读(1)  评论(0编辑  收藏  举报