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

文章介绍了如何在二叉搜索树中实现最小绝对差的迭代和递归计算,以及寻找二叉搜索树中的众数和最近公共祖先的两种方法。
摘要由CSDN通过智能技术生成

代码随想录算法训练营

今日任务
530.二叉搜索树的最小绝对差 ,501.二叉搜索树中的众数 ,236. 二叉树的最近公共祖先



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

//迭代法
/**
 * 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) {
        int min=100005;
        int lastrec=100005;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode top = stack.peek();
            if(top.left!=null){
                stack.push(top.left);
                top.left=null;
            }else{
                TreeNode pop = stack.pop();
                int sub=Math.abs(pop.val-lastrec);
                min=Math.min(min,sub);
                if(top.right!=null){
                    stack.push(top.right);
                    top.right=null;
                }
                lastrec=pop.val;
            }
        }
        return min;
    }
}
//递归法
/**
 * 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 {
    int min=100005;
    TreeNode pre;
    public int getMinimumDifference(TreeNode root) {
        if(root==null){
            return 0;
        }
        inorder(root);
        return min;
    }
    public void inorder(TreeNode root) {
        if(root==null){
            return;
        }
        inorder(root.left);
        if(pre!=null){
            min=Math.min(min,Math.abs(root.val-pre.val));
        }
        pre=root;
        inorder(root.right);
    }
}

501.二叉搜索树中的众数

/**
 * 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 {
    int count=1;
    int max=0;
    TreeNode pre;
    public int[] findMode(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        inorder(root,list);
        int size = list.size();
        int[] res=new int[size];
        for (int i = 0; i < res.length; i++) {
            res[i]=list.get(i);
        }
        return res;
    }
    public void inorder(TreeNode root,List<Integer> list) {
        if(root==null){
            return;
        }
        //pre是上一个节点
        inorder(root.left,list);
        //System.out.println(root.val);
        if(pre!=null){
            if(pre.val==root.val){
                count++;
            }else{
                count=1;
            }
            //说明root已经到了pre的后一个节点,count记录的是pre出现的次数
            if(count>max){
                //将原来list中存储的数据清空
                list.clear();
                list.add(pre.val);
                max=count;
            }else if(count==max){
                list.add(root.val);
            }
        }else{
            max= Math.max(max,count);
            list.add(root.val);
        }
        pre=root;
        inorder(root.right,list);
    }
}

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

//自己写的八知道什么方法
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    TreeNode parent;
    int pheight=-1;
    int qheight=-1;
    int ppos=-1;
    int qpos=-1;
    int height=0;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        List<Integer> list=new ArrayList<>();
        getDepth(root,p,q,list);
        return parent;
    }
    public int getDepth(TreeNode root,TreeNode p,TreeNode q,List<Integer> list) {
        if(root==null){
            return height;
        }
        height++;
        if(root.left!=null){
            int left=getDepth(root.left,p,q,list);
            if(left==-1){
                return -1;
            }
            height=height-1;
        }
        if(root.right!=null){
            int right=getDepth(root.right,p,q,list);
            if(right==-1){
                return -1;
            }
            height=height-1;
        }
        if(root.val==p.val){
            pheight=height;
            ppos=list.size();
        }else if(root.val==q.val){
            qheight=height;
            qpos=list.size();
        }
        if(pheight!=-1&&qheight!=-1){
            int min=Math.min(pheight,qheight);
            if(height<=min){
                boolean flag=true;
                if(list.contains(height)){
                    int minpos=Math.min(ppos,qpos);
                    int maxpos=Math.max(ppos,qpos);
                    for (int i = minpos; i < maxpos; i++) {
                        if(list.get(i)==height){
                            flag=false;
                            break;
                        }
                    }
                }
                if(flag){
                    parent=root;
                    height=-1;
                }
            }
        }
        list.add(height);
        //System.out.println(root.val+" "+height);
        return height;
    }
}
//递归法
/**
 * 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||root==p||root==q){
            return root;
        }
        //其实就是判断每个节点的左孩子和右孩子中有没有找到p和q
        //在左孩子中找到了一个,就去右孩子中找,如果右孩子中没有找到,则说明当前节点就是最近公共节点
        //同理,同理返回,返回
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        System.out.println(root.val);
        if(left==null&&right==null){
            return null;
        } else if (left != null && right == null) {
            return left;
        } else if (left == null && right != null) {
            return right;
        }else {
            return root;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值