赞
踩
目录
思路:由于二叉搜索树中序遍历就是一个就是一个有序数组,那么就转变成了在有序数组上求最小绝对差值。
在这个过程中,由于要比较,因此要用一个pre节点来记录前一个root,类似于双指针法
- class Solution {
- TreeNode pre;// 记录上一个遍历的结点
- int result = Integer.MAX_VALUE;
- public int getMinimumDifference(TreeNode root) {
- if(root==null)return 0;
- traversal(root);
- return result;
- }
- public void traversal(TreeNode root){
- if(root==null)return;
- //左
- traversal(root.left);
- //中
- if(pre!=null){
- result = Math.min(result,root.val-pre.val);
- }
- pre = root;
- //右
- traversal(root.right);
- }
- }
思路:
如果是普通二叉树,第一反应是用map存储
- 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;
- }
- }
- //把list里的元素转化成int类型数组
- 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);
- }
-
- }
但是已知这是二叉搜索树,所以第一反应是通过pre来进行比较
- class Solution {
- //全局变量
- int count = 0;
- int maxCount = 0;
- TreeNode pre = null;
- List<Integer> res = new ArrayList<>();
- public int[] findMode(TreeNode root) {
- traversal(root); //递归后res里已经得出了众数结果
- return res.stream().mapToInt(Integer::intValue).toArray();
- }
- public void traversal(TreeNode root){
- if(root == null){
- return;
- }
- traversal(root.left);
- //首先计算count
- if(pre == null || root.val != pre.val){
- count = 1;
- }else{
- count++;
- }
- //然后通过比较count和maxcount实现单次遍历找到众数
- if(maxCount == count){
- res.add(root.val);
- }else if(count > maxCount){
- maxCount = count;
- res.clear();
- res.add(root.val);
- }
- pre = root; //更新pre
- traversal(root.right);
- }
- }
最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。
那么既然要找公共祖先且深度要尽可能大,最方便就是自底向上查找
- class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- if(root==null | p == root | q == root) return root;
- TreeNode left = lowestCommonAncestor(root.left,p,q);
- TreeNode right = lowestCommonAncestor(root.right,p,q);
- if(left != null && right != null) return root;
- else if(left == null && right != null) return right;
- else if(left != null && right == null) return left;
- else return null;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。