代码随想录算法训练营day21 | 530.二叉搜索树的最小绝对差 ,501.二叉搜索树中的众数,236. 二叉树的最近公共祖先

530. 二叉搜索树的最小绝对差
package _06binary_tree.day21._01get_min_dif;

import _06binary_tree.TreeNode;

import java.util.Stack;

public class Solution {
    TreeNode pre = null;
    int result = Integer.MAX_VALUE;

    // 迭代
    public int getMinimumDifference2(TreeNode root) {
        if(root == null)return 0;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while (cur != null || !stack.isEmpty()){
            if(cur != null){
                stack.push(cur);
                cur = cur.left; // 左
            }else {
                cur = stack.pop();
                if(pre != null){
                    result = Math.min(result,cur.val - pre.val); // 中
                }
                pre = cur;
                cur = cur.right; // 右
            }
        }

        return result;
    }

    // 递归
    public int getMinimumDifference(TreeNode root) {
        if(root == null)return 0;
        traversal(root);
        return result;
    }

    private void traversal(TreeNode cur) {
        if(cur == null)return;
        traversal(cur.left); // 左
        if(pre != null){
            result = Math.min(result,cur.val - pre.val); // 中
        }
        pre = cur;
        traversal(cur.right); // 右
    }
}
501. 二叉搜索树中的众数
package _06binary_tree.day21._02find_mode;

import _06binary_tree.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution {
    int maxCount;
    int count;
    TreeNode pre;
    List<Integer> list;

    // 迭代
    public int[] findMode2(TreeNode root) {
        list = new ArrayList<>();
        count = 0;
        maxCount = 0;
        pre = null;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()){
            if(cur != null){
                stack.push(cur); // 左
                cur = cur.left;
            }else {
                cur = stack.pop(); // 中
                if(pre == null || pre.val != cur.val){
                    count = 1;
                }else {
                    count++;
                }

                // 更新maxCount
                if(count > maxCount){
                    maxCount = count;
                    list.clear();
                    list.add(root.val);
                }else if(count == maxCount){
                    list.add(root.val);
                }

                pre = cur;
                cur = cur.right; // 右
            }
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    // 递归
    public int[] findMode(TreeNode root) {
        list = new ArrayList<>();
        count = 0;
        maxCount = 0;
        pre = null;
        traverse(root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    private void traverse(TreeNode root) {
        if(root == null)return;
        // 左
        traverse(root.left);
        // 中
        if(pre == null || pre.val != root.val){
            count = 1;
        }else {
            count++;
        }

        // 更新maxCount
        if(count > maxCount){
            maxCount = count;
            list.clear();
            list.add(root.val);
        }else if(count == maxCount){
            list.add(root.val);
        }

        pre = root;

        // 右
        traverse(root.right);
    }
}
236. 二叉树的最近公共祖先
package _06binary_tree.day21._03lowest_common_ancestor;

import _06binary_tree.TreeNode;

public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left == null && right == null) return null;
        else if (left != null && right == null)return left;
        else if (right != null && left == null)return right;
        else return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值