力扣labuladong——一刷day69

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「遍历」的思维模式。

一、力扣669. 修剪二叉搜索树

/**
 * 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 TreeNode trimBST(TreeNode root, int low, int high) {
   
        if(root == null){
   
            return null;
        }
        if(root.val < low){
   
            return trimBST(root.right,low,high);
        }
        if(root.val > high){
   
            return trimBST(root.left, low, high);
        }
        root.left = trimBST(root.left, low, high);
        root.right = trimBST(root.right, low, high);
        return root;
    }
}

二、力扣671. 二叉树中第二小的节点

/**
 * 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 findSecondMinimumValue(TreeNode root) {
   
        if(root.left == null && root.right == null){
   
            return -1;
        }
        int left = root.left.val, right = root.right.val;
        if(root.val == root.left.val){
   
            left = findSecondMinimumValue(root.left);
        }
        if(root.val == root.right.val){
   
            right = findSecondMinimumValue(root.right);
        }
        if(left == -1){
   
            return right;
        }
        if(right == -1){
   
            return left;
        }
        return Math.min(left,right);
    }
}

相关推荐

  1. labuladong——day69

    2023-12-11 10:18:03       57 阅读
  2. labuladong——day68

    2023-12-11 10:18:03       56 阅读
  3. labuladong——day67

    2023-12-11 10:18:03       60 阅读
  4. labuladong——day66

    2023-12-11 10:18:03       51 阅读
  5. labuladong——day70

    2023-12-11 10:18:03       62 阅读
  6. labuladong——day74

    2023-12-11 10:18:03       55 阅读
  7. labuladong——day75

    2023-12-11 10:18:03       54 阅读
  8. labuladong——day76

    2023-12-11 10:18:03       69 阅读
  9. labuladong——day77

    2023-12-11 10:18:03       50 阅读
  10. labuladong——day78

    2023-12-11 10:18:03       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 10:18:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 10:18:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 10:18:03       82 阅读
  4. Python语言-面向对象

    2023-12-11 10:18:03       91 阅读

热门阅读

  1. GO语言使用OpenCV,图找图

    2023-12-11 10:18:03       55 阅读
  2. pytorch debug 常用工具

    2023-12-11 10:18:03       55 阅读
  3. kafka学习

    2023-12-11 10:18:03       49 阅读
  4. Vscode中配置SSH

    2023-12-11 10:18:03       54 阅读
  5. 力扣375周赛

    2023-12-11 10:18:03       54 阅读
  6. Python高级算法——贪心算法(Greedy Algorithm)

    2023-12-11 10:18:03       52 阅读
  7. MySQL中的数据类型

    2023-12-11 10:18:03       51 阅读
  8. MapReduce

    2023-12-11 10:18:03       36 阅读
  9. 哈顿矩阵:预防伤害的项目管理技术

    2023-12-11 10:18:03       44 阅读