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

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

作者: 激扬飞雪 | 来源:发表于2022-12-05 15:53 被阅读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;
    }
}

相关文章

  • 二叉树数据结构及其算法操作(Java)

    二叉树的定义 向二叉树中插入节点 搜索二叉树中最大值和最小值 搜索二叉树的深度(height)和节点数(size)...

  • 二叉搜索树

    二叉搜索树 图解二叉树搜索算法图解:二叉搜索树算法二叉查找树(Binary Search Tree),(又:二叉搜...

  • 2019 算法面试相关(leetcode)--树、二叉树、二叉搜

    翻转二叉树二叉树的前序遍历二叉树的中序遍历二叉树的后序遍历验证二叉搜索树二叉树的最近公共祖先二叉搜索树的最近公共祖...

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

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。(以下是参考他人的) 解:二叉树搜索树...

  • 树,二叉树,搜索树

    树,二叉树,搜索树 资料 二叉搜索树 Demo 树的遍历 Demo 题目 ◎ 二叉树的中序遍历 ◎ 二叉树...

  • 二叉树基础

    二叉树的分类 完全二叉树与满二叉树 二叉搜索树BST 平衡二叉搜索树BBST因为二叉搜索树有可能退化为链表,降低查...

  • 2019-10-22

    最优二叉树搜索算法。

  • 数据结构与算法之二叉搜索树(八)

    目录 二叉搜索树概念二叉搜索树的接口设计,包括增,删,改,查平衡二叉搜索树 一 二叉搜索树 二叉搜索树是二叉树的一...

  • [转]一文图解二叉树面试题

    原文:泥瓦匠-一文图解二叉树面试题 二叉树,搜索二叉树,是算法面试的必面题。聊聊面试点: 一、树 & 二叉树 树是...

  • 【数据结构】红黑树

    1、什么是红黑树? 红黑树是一个要求不那么严格的平衡二叉树搜索树(平衡二叉搜索树/AVL树=平衡二叉树+二叉搜索树...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/dpfifdtx.html