当前位置:   article > 正文

二叉搜索树的众数(递归,迭代)Java_二叉搜索树中的众数 java

二叉搜索树中的众数 java

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:

结点左子树中所含结点的值小于等于当前结点的值
结点右子树中所含结点的值大于等于当前结点的值
左子树和右子树都是二叉搜索树

 暴力递归法:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. public int[] findMode(TreeNode root) {
  18. Map<Integer,Integer> map=new HashMap<>();
  19. List<Integer> list=new ArrayList<>();
  20. if(root==null) return list.stream().mapToInt(Integer::intValue).toArray();
  21. searchBST(root,map);
  22. List<Map.Entry<Integer,Integer>> mapList=map.entrySet().stream().sorted((c1,c2)->c2.getValue().compareTo(c1.getValue())).collect(Collectors.toList());
  23. list.add(mapList.get(0).getKey());
  24. for(int i=1;i<mapList.size();i++){
  25. if(mapList.get(i).getValue()==mapList.get(i-1).getValue()){
  26. list.add(mapList.get(i).getKey());
  27. }else{
  28. break;
  29. }
  30. }
  31. return list.stream().mapToInt(Integer::intValue).toArray();
  32. }
  33. void searchBST(TreeNode node,Map<Integer,Integer> map){
  34. if(node==null) return;
  35. map.put(node.val,map.getOrDefault(node.val,0)+1);
  36. searchBST(node.left,map);
  37. searchBST(node.right,map);
  38. }
  39. }

优化递归:利用二叉搜索树的特点。

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. ArrayList<Integer> resList;
  18. int maxCount;
  19. int count;
  20. TreeNode pre;
  21. public int[] findMode(TreeNode root) {
  22. resList=new ArrayList<>();
  23. maxCount=0;
  24. count=0;
  25. pre=null;
  26. find(root);
  27. int []res=new int[resList.size()];
  28. for(int i=0;i<resList.size();i++){
  29. res[i]=resList.get(i);
  30. }
  31. return res;
  32. }
  33. public void find(TreeNode root){
  34. if(root==null) return;
  35. find(root.left);
  36. int rootValue=root.val;
  37. if(pre==null||rootValue!=pre.val){
  38. count=1;
  39. }else{
  40. count++;
  41. }
  42. if(count>maxCount){
  43. resList.clear();
  44. resList.add(rootValue);
  45. maxCount=count;
  46. }else if(count==maxCount) resList.add(rootValue);
  47. pre=root;
  48. find(root.right);
  49. }
  50. }

迭代:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. public int[] findMode(TreeNode root) {
  18. Stack<TreeNode> stack=new Stack<>();
  19. TreeNode cur=root;
  20. TreeNode pre=null;
  21. int maxCount=0;
  22. int count=0;
  23. List<Integer> list=new ArrayList<>();
  24. while(cur!=null||!stack.isEmpty()){
  25. if(cur!=null){
  26. stack.push(cur);
  27. cur=cur.left;
  28. }else{
  29. cur=stack.pop();
  30. if(pre==null) count=1;
  31. else if(pre.val==cur.val) count++;
  32. else count=1;
  33. if(count==maxCount) list.add(cur.val);
  34. if(count>maxCount){
  35. maxCount=count;
  36. list.clear();
  37. list.add(cur.val);
  38. }
  39. pre=cur;
  40. cur=cur.right;
  41. }
  42. }
  43. return list.stream().mapToInt(Integer::intValue).toArray();
  44. }
  45. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/882513
推荐阅读
相关标签
  

闽ICP备14008679号