算法|二叉搜索树的最小绝对差、二叉搜索树中的众数、二叉树的最近公

2022-12-05  本文已影响0人  激扬飞雪

530. 二叉搜索树的最小绝对差

题目连接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
思路一:使用中序遍历,前后两个之差是最小的绝对差 (递归法)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private TreeNode pre;
    private int minV = Integer.MAX_VALUE;
    private void getMin(TreeNode root) {
        if (root == null) return;
        getMin(root.left);
        if (pre != null) minV = Math.min(minV, root.val - pre.val);
        pre = root;
        getMin(root.right);
    }
    public int getMinimumDifference(TreeNode root) {
      getMin(root);
      return minV;
    }
}

思路二:使用迭代法中序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int getMinimumDifference(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode pre = null;
        int minV = Integer.MAX_VALUE;
        while (cur != null || !stack.isEmpty()){
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                TreeNode treeNode = stack.pop();
                if (pre != null) minV = Math.min(minV, treeNode.val - pre.val);
                pre = treeNode;
                cur = treeNode.right;
            }
        }
        return minV;
    }
}

二、 501. 二叉搜索树中的众数

题目连接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/
思路一:使用二叉搜索树中序遍历单调递增有序的特性,使用中序遍历,统计出现的频次,前后相等的count++,前后不相等的或者第一个元素count = 1; if (count == maxCount) 将这个元素加入结合中,如果有比maxcount更大的值,则清楚集合,加入新集合
1、递归法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private TreeNode pre;
    private int count = 0;
    private int maxCount = 0;
    private void findMode(TreeNode root, List<Integer> list) {
        if (root == null) return ;
        findMode(root.left, list);
        if (pre == null) {
            count = 1;
        } else if (pre.val == root.val) {
            count++;
        } else {
            count = 1;
        }
        if (count == maxCount) {
            list.add(root.val);
        } else if (count > maxCount){
            list.clear();
            list.add(root.val);
            maxCount = count;
        }
        pre = root;
        findMode(root.right, list);
    }
    public int[] findMode(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        findMode(root, list);
        int[] result = new int[list.size()];
        for (int i = 0; i < result.length; i++){
            result[i] = list.get(i);
        }
        return result;
    }
}

2、使用迭代法中序

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int[] findMode(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        int count = 0;
        int maxCount = 0;
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()){
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                TreeNode treeNode = stack.pop();
                if (pre == null || pre.val != treeNode.val) {
                    count = 1;
                } else {
                    count++;
                }
                if (count == maxCount) {
                    list.add(treeNode.val);
                } else if (count > maxCount) {
                    list.clear();
                    list.add(treeNode.val);
                    maxCount = count;
                }
                pre = treeNode;
                cur = treeNode.right;
            }
        }

        int[] result = new int[list.size()];
        for (int i = 0; i < result.length; i++){
            result[i] = list.get(i);
        }
        return result;
    }
}

236. 二叉树的最近公共祖先

题目连接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
思路:最新公共祖先,使用后序遍历,左边不为空,右边不为空返回中间节点,即使最近公共祖先

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (root == p || root == q) return root;
        TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
        TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
        if (leftNode != null && rightNode != null) return root;
        else if (leftNode != null && rightNode == null) return leftNode;
        else if (leftNode == null && rightNode != null) return rightNode;
        else return null;
    }
}
上一篇下一篇

猜你喜欢

热点阅读