随笔分类 - DFS
深搜
摘要:代码一 class Solution(object): # 思路:用前序式DFS,从root到叶子。 def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return
阅读全文
摘要:class Solution(object): def isUnivalTree(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.dfs(root) def dfs
阅读全文
摘要:代码一 用list或者字符串方式返回二叉树的所有路径:https://www.cnblogs.com/panweiwei/p/13752895.html class Solution(object): def pathSum(self, root, sum): """ :type root: Tre
阅读全文
摘要:一、用list返回二叉树的所有路径 class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def findAllPa
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路:层序模板。 def maxDepth(self, root): """ :type
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def kthLargest(self, root, k): """ :type
阅读全文
摘要:class Solution(object): def isCousins(self, root, x, y): """ :type root: TreeNode :type x: int :type y: int :rtype: bool """ # 用字典记录每个节点的父节点、以及该节点的深度(
阅读全文
摘要:DFS应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def increasingBST(self, root): """ :type root: TreeNod
阅读全文
摘要:class Solution(object): def leafSimilar(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ res1 = [] self.dfs(root1
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def maxDepth(self, root): """ :type root: Node
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def findBottomLeftValue(self, root): """
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def kthSmallest(self, root, k): """ :type
阅读全文