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

 530.二叉搜索树的最小绝对差Minimum Absolute Difference in BST - LeetCode

方法一:中序遍历转成数组,再求最小值

方法二:双指针
int res = Integer.MAX_VALUE;
TreeNode pre = null;
void traversal(TreeNode cur);
左:traversal(root.left)
中:if(pre != null)
                res = min(res, cur.val - pre.val);
        pre = cur;
        traversal(root.right)
右:traversal(cur.right)
/**
 * 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 {
    TreeNode pre;
    int min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) return 0;
        traversal(root);
        return min;
    }

    public void traversal(TreeNode cur) {
        if (cur == null) return;
        traversal(cur.left);
        if (pre != null) {
            min = Math.min(min, cur.val - pre.val);
        }
        pre = cur;
        traversal(cur.right);
    }
}

501.二叉搜索树中的众数 Find Mode in Binary Search Tree - LeetCode

TreeNode pre;

int count = 0;//统计单个出现频率

int maxCount = 0;//出现的最高频率

List<> res ;

void traverdal(TreeNode cur)

        if (cur == null) return;

左    traversal(cur.left);

        if(pre == null) count = 1;

        else if (pre == cur.val) count++;

        else count = 1;

        pre = cur;

        if (count == maxCount) res.add(cur.val);

        if (count > maxCount)

                maxCount = count;

                res.clear();

                res.add(cur.val);

        traversal(cur.right)

return;

/**
 * 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 {
    TreeNode pre;
    int count;
    int maxCount;
    List<Integer> list;
    public int[] findMode(TreeNode root) {
        list = new ArrayList<>();
        count = 0;
        maxCount = 0;
        pre = null;
        traversal(root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    private void traversal(TreeNode cur) {
        if (cur == null) return;
        traversal(cur.left);
        if (pre == null) {
            count = 1;
        } else if (pre.val == cur.val) {
            count++;
        } else {
            count = 1;
        }
        pre = cur;
        if (count == maxCount) {
            list.add(cur.val);
        }
        if (count > maxCount) {
            maxCount = count;
            list.clear();
            list.add(cur.val);
        }
     //   pre = cur;
        traversal(cur.right);

        return;
    }
}

236. 二叉树的最近公共祖先 Lowest Common Ancestor of a Binary Tree - LeetCode

TreeNode traversal(TreeNode root, p, q)

        if (root == null) return root;

        if (root == p || root == q) return root;

        TreeNode left = traversal(root.left, p, q);

        TreeNode right = traversal(root.right, p, q);

        中 if(left != null && right != null) return root;

        if (left == null && right != null) return right;

        else (left != null && right == null) return left;

        else return null;

/**
 * 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;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left == null) {
            return right;
        } else if (right == null) {
            return left;
        } 
            return root;
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值