随笔分类 - DFS
[Leetcode 111]二叉树的最短深度 BFS/DFS
摘要:题目 给定二叉树,求最短路径包含的节点个数 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minimum depth. The minimum depth is th
阅读全文
[Leetcode 108]有序数组转BST二叉搜索树Convert Sorted Array to Binary Search Tree
摘要:题目 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Given an integer array nums where the elements are sorted in ascending or
阅读全文
[Leetcode 22]生成括号generate parentheses
摘要:题目 给定括号对数n,生成所有可能的标准括号结果* *指不能)( https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all co
阅读全文
[Leetcode 559]N叉树的最大深度Maximum Depth of N-ary Tree DFS/BFS模板
摘要:题目 https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ N叉树的最大深度 Given a n-ary tree, find its maximum depth. The maximum depth is the number of
阅读全文
[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree
摘要:题目 求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+1(本身这个点 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given the root of a binary tree, return its maxi
阅读全文
[Leetcode 124]二叉树最大权重路径和 Binary Tree Maximum Path Sum
摘要:题目 与543相似,543是求最长路径,这是最大权重求和(路径不一定最长) A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge con
阅读全文
[Leetcode 543]二叉树的直径Diameter of Binary Tree
摘要:题目 二叉树的直径,即是求树中任意俩点间的最大距离(深度) Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the len
阅读全文
[Leetcode 130]被围区域翻牌Surrounded Regions
摘要:【题目】 其实就是矩阵里,除了与最外层边O相连的O不被翻牌成X外,其他O全部翻牌成X Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded
阅读全文
[Leetcode 695]岛屿最大面积 Max Area of Island DFS
摘要:【题目】 岛屿矩阵,0表示海洋,数字表示岛屿面积,只有上下左右代表岛屿相连,对角线不算。 求问海洋里最大的岛屿面积 You are given an m x n binary matrix grid. An island is a group of 1's (representing land) c
阅读全文
[Leetcode 394]编译解码字符串Decode String
摘要:【题目】 将格式为 数[数[字母字母]数[字母]] 的字符串展开 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string
阅读全文
[Leetcode 37]*数独游戏 Sudoku Solver 附解释
摘要:【题目】 每一行、每一列、每个3*3的格子里只能出现一次1~9。 【思路】 参考了思路,附加了解释。 dfs遍历所有非空格子,n是已经填好的个数。 初始化条件。n=81,都填了,返回结束。对于已经填好的b[x][y] != '.'跳过,到下一个。 xy的设计保证先行后列填写。 开始填数字,valid
阅读全文
[Leetcode 108]有序数组转二叉搜索树Convert Sorted Array to Binary Search Tree
摘要:【题目】 给出的升序排序的数组,个数必为奇数,要求形成二叉搜索(平衡)树。 【思路】 辅助函数fun,[0,len]=>[0,mid-1]+[mid+1,len]。 当left>right,返回null。 【AC代码】
阅读全文
[Leetcode 105]*前序后序遍历形成树
摘要:so basically flag-start is size of the roots left subtree, therefore to get the start of right subtree you gotta get to the start of the first value o
阅读全文
[Leetcode 101]判断对称树 Symmetric Tree
摘要:【题目】 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is s
阅读全文
[Leetcode 100]判断二叉树相同 Same Tree
摘要:【题目】 判断二叉树是否相同。 【思路】 check函数。 p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p、q其中一方更短) p.val==q.val,值相同,继续迭代向左向右遍历check(p.left,q.left)&&ch
阅读全文
浙公网安备 33010602011771号