leetcode深度优先搜索DFS——938/78/200

一、938二叉搜索树的范围和

1.题目

https://leetcode-cn.com/problems/range-sum-of-bst/
给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

2.思路

1.深度优先搜索(可套用二叉树的前序遍历)
2.可利用二叉搜索(查找)树的特性:左子树<根节点<右子树

3.代码

1.二叉树前序遍历

/**
 * 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 rangeSumBST(TreeNode root, int low, int high) {
        //题目的意思是返回值在[L,H]区间的值的总和,即L<=X<=H
        int res = 0;
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        //stack.add(cur);//如果root满足条件的话,会多加一次,注意取消
        while(cur!=null || !stack.isEmpty()){
            while (cur!=null){
                if(cur.val >=low && cur.val <= high){
                    res += cur.val;
                }
                stack.add(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            cur = cur.right;
        }
        return res;
    }
}

2.官方题解

/**
 * 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 rangeSumBST(TreeNode root, int low, int high) {
        //题目的意思是返回值在[L,H]区间的值的总和,即L<=X<=H
        if (root == null) return 0;
        if (root.val <low) return rangeSumBST(root.right,low,high);
        if(root.val > high) return rangeSumBST(root.left,low,high);
        return root.val+rangeSumBST(root.left,low,high)+rangeSumBST(root.right,low,high);
    }
}

二、78子集

可参考回溯:https://blog.csdn.net/Xiao__Bei/article/details/121754875

三、200岛屿数量

1.题目

https://leetcode-cn.com/problems/number-of-islands/
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

2.思路

可参考官网题解里大神的解题思路
https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/

3.代码

class Solution {
    public void dfs(char[][] grid, int c, int r){
        //base case
        if (c<0||c>=grid.length||r<0||r>=grid[0].length){
            return;
        }
        if(grid[c][r] !='1') return;
        grid[c][r] = '2';
        dfs(grid,c-1,r);
        dfs(grid,c+1,r);
        dfs(grid,c,r-1);
        dfs(grid,c, r+1);
    }
    
    public int numIslands(char[][] grid) {
        int isLand = 0;
        for(int i =0; i<grid.length;i++){
            for(int j =0; j<grid[0].length;j++){
                if(grid[i][j] == '1'){
                    isLand++;
                    dfs(grid,i,j);
                }
            }
        }
        return isLand;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值