深度优先搜索
Ealiser
这个作者很懒,什么都没留下…
展开
-
LeetCode第669题 修剪二叉搜索树
算法深度优先搜索核心思想主要还是如何减枝比较重要。代码class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if (root == null) return null; if(root.val > high || root.val < low) { root = del(root); root .原创 2022-04-25 11:08:48 · 1947 阅读 · 0 评论 -
LeetCode第114题 二叉树展开为链表
算法核心思想代码class Solution { public void flatten(TreeNode root) { if (root == null) return; List<TreeNode> temp = new ArrayList<>(); dfs(root,temp); for (int i = 1; i < temp.size(); ++i) { ..原创 2022-04-16 11:51:03 · 2941 阅读 · 0 评论 -
LeetCode第113题 路径总和II
算法回溯和深度优先搜索核心思想二叉树很适合用深度优先搜索去做,简单快捷。这道题值得注意点是必须从根到叶子,同时没必要减枝,减枝的条件也有点复杂。代码class Solution { List<List<Integer>> res = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int targetSum) { .原创 2022-04-15 16:14:03 · 1835 阅读 · 0 评论 -
LeetCode第200题 岛屿数量
算法深度优先搜索核心思想简单的深度优先搜索,每次搜索就发现一个岛屿。这个解法是岛屿题的通解题。代码class Solution { public int numIslands(char[][] grid) { int nums = 0; int m = grid.length; int n = grid[0].length; for(int i = 0;i < m;++i){ for(int .原创 2022-04-06 15:50:54 · 1194 阅读 · 0 评论