代码随想录算法训练营第二十二天 _ 二叉树_235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点、129. 求根节点到叶节点数字之和。

学习目标:

60天训练营打卡计划!

学习内容:

235. 二叉搜索树的最近公共祖先

  • 充分的利用二叉搜索树的性质:有确定的搜索方向;且当根节点落在p和q之间时,该根节点一定是最近公共祖先。
  • 236的普通思路依旧适用于该题。
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null)  return root;

        if(root.val > p.val && root.val > q.val){
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            // 不为空则说明找到了当前节点
            // left即为最近公共节点
            if(left != null)
                return left;
        }
        else if(root.val < p.val && root.val < q.val){
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if(right != null)
                return right;
        }
        return root;
    }
}

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null)  return root;
        if(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 root;
        if(left != null && right == null)  return left;
        if(left == null && right != null)  return right;

        return null;
    }
}

701.二叉搜索树中的插入操作

  • 在叶子节点处加入新节点,在递归结束处对节点进行增加操作。
  • 题目中说了加入的新节点是原树中没有的,否则就不加入。
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        // 确定递归的结束条件  --- 在叶子节点处加入新的节点
        if(root == null){
            TreeNode node = new TreeNode(val);
            return node;
        }

        // 通过等式将node节点加入原二叉树中
        if(val < root.val)
            root.left = insertIntoBST(root.left, val);
        if(val > root.val)
            root.right = insertIntoBST(root.right, val);

        return root;
    }
}

450.删除二叉搜索树中的节点

  • 在结束条件时,分情况对节点进行删除。可以分为以下的5种情况:
    ①未找到要删除的节点;
    ②要删除的节点是叶子节点(无左无右)
    ③要删除的节点有左节点
    ④要删除的节点有右节点
    ⑤要删除的节点既有左节点也有右节点。
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        // 先写终止条件
        if(root == null)  return null;

        if(root.val == key){
            if(root.left == null && root.right == null)   return null;
            if(root.left != null && root.right == null)   return root.left;
            if(root.left == null && root.right != null)   return root.right;
            // 将该节点的左子树挂到右子树最大深度最左侧的叶子节点下
            if(root.left != null && root.right != null){
                TreeNode node = root.right;
                while(node.left != null)   node = node.left;
                node.left = root.left;
                return root.right;
            }
        }

        // 最小递归的实现
        if(root.val > key)   root.left = deleteNode(root.left, key);
        if(root.val < key)   root.right = deleteNode(root.right, key);

        return root;
    }
}

1365. 有多少小于当前数字的数字

  • 先对传入的数组排序后即可得到小于当前数字的数字。
  • 熟悉对数组的复制和排序api
    Arrays.sort(sortednums);
    Arrays.copyOf(nums, nums.length);
class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        
        int[] sortednums = Arrays.copyOf(nums, nums.length);
        Arrays.sort(sortednums);
        int[] res = new int[nums.length];
        int j = 0;

        for(int i = 0; i < nums.length; i++){
            while(sortednums[j] != nums[i]){
                j++;
            }
            res[i] = j;
            j = 0;
        }

        return res;
    }
}

129. 求根节点到叶节点数字之和

  • 没想清楚的就是为什么结束条件变成了node.left == null && node.right == null?
    ① node == null 作为终止条件:
    当问题需要遍历整棵树的所有节点时,通常使用 node == null 作为终止条件。这种情况适用于遍历整个树的场景,比如树的搜索、求深度、路径遍历等。
    ② node.left == null && node.right == null 作为终止条件:
    当问题需要针对叶子节点进行特定操作时,例如寻找叶子节点、计算叶子节点的高度或路径、使用叶子结点的值等,此时使用 node.left == null && node.right == null 作为终止条件更为合适。
class Solution {
    int allSum = 0;
    int sum = 0;
    List<Integer> list = new ArrayList<>();

    private int Array2Int(List<Integer> lis){
        int res = 0;
        for(int i = 0; i < lis.size(); i++){
            res = res * 10 + lis.get(i);
        }
        return res;
    }

    private void traversal(TreeNode node){
        // 结束条件
        if(node.left == null && node.right == null){
            allSum += Array2Int(list);
            return;
        }

        if(node.left != null){
            list.add(node.left.val);
            traversal(node.left);
            list.remove(list.size() - 1);
        }

        if(node.right != null){
            list.add(node.right.val);
            traversal(node.right);
            list.remove(list.size() - 1);
        }

        return ;
    }

    public int sumNumbers(TreeNode root) {
        list.add(root.val);
        traversal(root);
        return allSum;
    }
}

学习时间:

  • 上午一小时,下午一个半小时,整理文档半小时。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值