![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
DFS-树
Alwaysion
more persistent for nothing
个人qq:790693499,欢迎来交流
展开
-
【力扣856. 括号的分数】分治+栈(Python3)
题目描述https://leetcode-cn.com/problems/score-of-parentheses/思路题解https://leetcode-cn.com/problems/score-of-parentheses/solution/gua-hao-de-fen-shu-by-leetcode/方法1:分治法class Solution(object): def scoreOfParentheses(self, S): def F(i, j):原创 2021-08-22 17:13:14 · 87 阅读 · 0 评论 -
【京东笔试题】熊猫吃竹子,回溯
题目描述输入n,a,b,c,n代表柱子长度,a,b,c代表可以切割的大小。求这个竹子最多能被分成多少分(不能有剩余)思路题解回溯法def recall(count, base, res,s): #base没有被压入 if count -base == 0: print(s+str(base)+' ') return res+1 if count - base>0: x = recall(count - base, a, r原创 2021-08-21 22:00:55 · 465 阅读 · 0 评论 -
【力扣40. 组合总和 II】回溯(python3)
题目描述https://leetcode-cn.com/problems/combination-sum-ii/思路题解一开始的错误做法:会卡在相同的小的数字特别多的情况:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]27[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1原创 2021-08-11 16:48:01 · 146 阅读 · 0 评论 -
【力扣剑指 Offer 36. 二叉搜索树与双向链表、 426 】取头尾拼接的dfs+标记遍历的dfs(python3)
题目描述https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/思路题解取头尾拼接的dfs自己做的首先后续遍历,得到左右子树的链表的头尾节点,然后在把中间节点和他们拼接起来,最后返回新的头尾节点。"""# Definition for a Node.class Node: def __init__(self, val, left=None, right=None):原创 2021-07-16 17:24:21 · 329 阅读 · 0 评论 -
树的公共祖先总结(236. 二叉树的最近公共祖先、235. 二叉搜索树的最近公共祖先)
目录236. 二叉树的最近公共祖先235. 二叉搜索树的最近公共祖先递归1递归2迭代236. 二叉树的最近公共祖先236. 二叉树的最近公共祖先class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': def dfs(root): if not root or root==p or roo原创 2021-07-13 10:44:52 · 161 阅读 · 0 评论 -
【力扣剑指 Offer 54. 二叉搜索树的第k大节点】dfs直接返回(python3)
题目描述https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/思路题解dfs直接返回法class Solution: def kthLargest(self, root: TreeNode, k: int) -> int: def dfs(root): nonlocal k if not root:return 0原创 2021-07-10 17:57:56 · 86 阅读 · 0 评论 -
【力扣78. 子集】回溯+二进制(python3)
题目描述https://leetcode-cn.com/problems/subsets/思路题解回溯class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: ans=[[]] def dfs(i,pre): if i==len(nums):return ans.append(pre) for j i原创 2021-07-07 16:57:48 · 105 阅读 · 0 评论 -
【力扣538. 把二叉搜索树转换为累加树】递归(python3)
题目描述https://leetcode-cn.com/problems/convert-bst-to-greater-tree/思路题解死活没想到怎么让右边的最大的节点获取到左边最小的节点,看了答案发现直接一个全局变量保存当前的最大值解决了。因为是逆中序遍历,顺序是右、中、左,因此遍历的值一定是单调递减的。class Solution: def convertBST(self, root: TreeNode) -> TreeNode: def dfs(root: Tr原创 2021-07-07 14:30:08 · 80 阅读 · 0 评论 -
【力扣93. 复原 IP 地址】递归(Python3)
题目描述https://leetcode-cn.com/problems/restore-ip-addresses/思路题解直接dps,记得剪枝class Solution: def restoreIpAddresses(self, s: str) -> List[str]: ans=[] def dfs(p,i,s,str): if i>=len(s) or p>4:return if p==原创 2021-07-06 15:36:21 · 122 阅读 · 0 评论 -
【力扣剑指 Offer 17. 打印从1到最大的n位数】大数计算(Python3)
题目描述https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/思路题解https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/solution/mian-shi-ti-17-da-yin-cong-1-dao-zui-da-de-n-wei-2/可以直接返回list,但是本题的本意是考察大数计算的class Sol原创 2021-07-05 10:59:34 · 82 阅读 · 0 评论 -
【力扣207. 课程表】bfs拓扑排序+dfs 判断是否有向图是否存在环(Python3)
题目描述https://leetcode-cn.com/problems/course-schedule/思路题解https://leetcode-cn.com/problems/course-schedule/solution/course-schedule-tuo-bu-pai-xu-bfsdfsliang-chong-fa/BFS拓扑排序from collections import dequeclass Solution: def canFinish(self, numCour原创 2021-07-01 17:33:41 · 419 阅读 · 0 评论 -
【力扣236. 二叉树的最近公共祖先】递归(Python3)
题目描述https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/思路题解# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass原创 2021-07-01 13:35:16 · 276 阅读 · 0 评论 -
【力扣剑指 Offer 28. 对称的二叉树】递归(Python3)
题目描述https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/思路题解相当于分两个指针,分别遍历。一个中左右,一个中右左https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/solution/mian-shi-ti-28-dui-cheng-de-er-cha-shu-di-gui-qing/# Definition for a binary tree nod原创 2021-07-01 10:39:13 · 73 阅读 · 0 评论 -
【394. 字符串解码】辅助栈+递归(python3)
题目描述https://leetcode-cn.com/problems/decode-string/思路题解辅助栈https://leetcode-cn.com/problems/decode-string/solution/decode-string-fu-zhu-zhan-fa-di-gui-fa-by-jyd/class Solution: def decodeString(self, s: str) -> str: stack, res, multi = [原创 2021-06-28 09:42:29 · 91 阅读 · 0 评论 -
【力扣463. 岛屿的周长】dfs(python3)
题目描述https://leetcode-cn.com/problems/island-perimeter/思路题解注意在类里面全局变量的使用。这里使用了self.c。如果用global,必须先赋值才可以。python3全局变量的使用经验总结class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: self.c=0 def is_edge(grid,i,j):原创 2021-06-18 20:52:06 · 163 阅读 · 0 评论 -
【力扣200. 岛屿数量 305. 岛屿数量 II】DFS+BFS+并查集、并查集(python3)
题目描述https://leetcode-cn.com/problems/number-of-islands/思路题解https://leetcode-cn.com/problems/number-of-islands/solution/number-of-islands-shen-du-you-xian-bian-li-dfs-or-/原创 2021-06-18 15:05:49 · 414 阅读 · 0 评论 -
【力扣114-二叉树展开为链表】dfs时间复杂度O(1) (golang)
目录题目描述思路题解题目描述https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/思路题解dfs,用lt,rt保存当前子树遍历的结果。用lt_end、rt_end保存当前子树的最后一个节点。注意空指针。/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode *原创 2021-06-16 14:26:50 · 360 阅读 · 0 评论 -
【力扣105-从前序与中序遍历序列构造二叉树】dfs(golang)
目录题目描述思路题解题目描述https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/思路题解/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func原创 2021-06-15 21:52:50 · 124 阅读 · 0 评论 -
【力扣17-电话号码的字母组合】dfs(golang)
目录题目描述思路题解题目描述https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/思路题解dfs法,很简单唯一的坑在于slice的传参,要记住传指针。否则在单个测试用例是没问题的,提交就不行。原因应该是由于后台循环调用程序导致。import ( "strconv")func letterCombinations(digits string) []string { var ans []s原创 2021-05-19 22:18:50 · 84 阅读 · 0 评论 -
【力扣97-二叉树的中序遍历】多种解法:栈+dfs(golang)
目录题目描述思路题解栈递归题目描述https://leetcode-cn.com/problems/binary-tree-inorder-traversal/思路题解栈/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func inorderTraversal(root *原创 2020-12-12 21:00:32 · 237 阅读 · 0 评论 -
【力扣617-合并二叉树】DFS(python3)
目录题目描述思路题解题目描述思路题解二话不说,直接递归,每次传入当前两棵树的指针状态,相当于同时同位遍历两棵二叉树当有任何一方为空时,返回不为空的那个or空当均不为空时,左子树+=右子树,左子树=dfs(各自的左子树),右子树=dfs(各自的右子树)代码如下:# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x#原创 2020-11-26 21:59:15 · 192 阅读 · 0 评论