当前位置:   article > 正文

【Java】代码随想录二叉树07| LeetCode530 二叉搜索树的最小绝对差、LeetCode501二叉搜索树中的众数、LeetCode236二叉树的最近公共组先

【Java】代码随想录二叉树07| LeetCode530 二叉搜索树的最小绝对差、LeetCode501二叉搜索树中的众数、LeetCode236二叉树的最近公共组先

目录

二叉树07

LeetCode530 二叉搜索树的最小绝对差

LeetCode501 二叉搜索树中的众数

LeetCode236 二叉树的最近公共组先


二叉树07

LeetCode530 二叉搜索树的最小绝对差

思路:由于二叉搜索树中序遍历就是一个就是一个有序数组,那么就转变成了在有序数组上求最小绝对差值。

在这个过程中,由于要比较,因此要用一个pre节点来记录前一个root,类似于双指针法

  1. class Solution {
  2. TreeNode pre;// 记录上一个遍历的结点
  3. int result = Integer.MAX_VALUE;
  4. public int getMinimumDifference(TreeNode root) {
  5. if(root==null)return 0;
  6. traversal(root);
  7. return result;
  8. }
  9. public void traversal(TreeNode root){
  10. if(root==null)return;
  11. //左
  12. traversal(root.left);
  13. //中
  14. if(pre!=null){
  15. result = Math.min(result,root.val-pre.val);
  16. }
  17. pre = root;
  18. //右
  19. traversal(root.right);
  20. }
  21. }

LeetCode501 二叉搜索树中的众数

思路:

如果是普通二叉树,第一反应是用map存储

  1. class Solution {
  2. public int[] findMode(TreeNode root) {
  3. Map<Integer, Integer> map = new HashMap<>();
  4. List<Integer> list = new ArrayList<>();
  5. if (root == null) return list.stream().mapToInt(Integer::intValue).toArray();
  6. // 获得频率 Map,这是降序,所以数组的第一个元素就是频率最高的元素
  7. searchBST(root, map);
  8. List<Map.Entry<Integer, Integer>> mapList = map.entrySet().stream()
  9. .sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue()))
  10. .collect(Collectors.toList());
  11. list.add(mapList.get(0).getKey());
  12. // 把频率最高的加入 list,并且可能有频率相同的多个众数
  13. for (int i = 1; i < mapList.size(); i++) {
  14. if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) {
  15. list.add(mapList.get(i).getKey());
  16. } else {
  17. break;
  18. }
  19. }
  20. //把list里的元素转化成int类型数组
  21. return list.stream().mapToInt(Integer::intValue).toArray();
  22. }
  23. void searchBST(TreeNode curr, Map<Integer, Integer> map) {
  24. if (curr == null) return;
  25. map.put(curr.val, map.getOrDefault(curr.val, 0) + 1);
  26. searchBST(curr.left, map);
  27. searchBST(curr.right, map);
  28. }
  29. }

但是已知这是二叉搜索树,所以第一反应是通过pre来进行比较

  • 仍然使用中序遍历
  • 中间的逻辑:比较当前节点和pre,如果pre为空,则是第一个节点,count==1;如果与pre和cur的值相同,则count++;如果不相同,则count还是等于1
  • 得到频率数组后,怎么得到众数:普通的做法(两次遍历)——首先遍历频率数组找到最大频率maxCount,然后再遍历一遍找到最大频率maxCount对应的数,放入结果集。一次遍历——如果count等于maxCount,把对应的数加入res数组;如果大于,则更新maxCount,清空res数组,重新加入众数。
  1. class Solution {
  2. //全局变量
  3. int count = 0;
  4. int maxCount = 0;
  5. TreeNode pre = null;
  6. List<Integer> res = new ArrayList<>();
  7. public int[] findMode(TreeNode root) {
  8. traversal(root); //递归后res里已经得出了众数结果
  9. return res.stream().mapToInt(Integer::intValue).toArray();
  10. }
  11. public void traversal(TreeNode root){
  12. if(root == null){
  13. return;
  14. }
  15. traversal(root.left);
  16. //首先计算count
  17. if(pre == null || root.val != pre.val){
  18. count = 1;
  19. }else{
  20. count++;
  21. }
  22. //然后通过比较count和maxcount实现单次遍历找到众数
  23. if(maxCount == count){
  24. res.add(root.val);
  25. }else if(count > maxCount){
  26. maxCount = count;
  27. res.clear();
  28. res.add(root.val);
  29. }
  30. pre = root; //更新pre
  31. traversal(root.right);
  32. }
  33. }

LeetCode236 二叉树的最近公共组先

最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。

那么既然要找公共祖先且深度要尽可能大,最方便就是自底向上查找

  • 回溯就是从低向上的过程,且后序遍历(左右中)就是天然的回溯过程,可以根据左右子树的返回值,来处理中节点的逻辑。
  • 最近公共祖先:(情况1)如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。(情况2)p在q的子树或者q在p的子树。
  • 构造递归函数:首先,传入参数是root,p,q,返回值是最近公共祖先Treenode;其次,终止条件是root为空,除此之外还有p、q为root时都返回root;单层递归是这样的,左,右,中间对left和right进行逻辑处理,即左右都不为空,return root;左或右单边为空,返回不为空的右或左;左中右都为空,返回左右都行。
  1. class Solution {
  2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3. if(root==null | p == root | q == root) return root;
  4. TreeNode left = lowestCommonAncestor(root.left,p,q);
  5. TreeNode right = lowestCommonAncestor(root.right,p,q);
  6. if(left != null && right != null) return root;
  7. else if(left == null && right != null) return right;
  8. else if(left != null && right == null) return left;
  9. else return null;
  10. }
  11. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号