随笔分类 - Python
摘要:class Solution(object): def sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 return self.dfs(root, 0) def dfs(se
阅读全文
摘要:class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: None Do not return anything, modify root in-place instead. """ if no
阅读全文
摘要:方法一 class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ ans = [] if not root: re
阅读全文
摘要:class Solution(object): def hasPathSum(self, root, sumt): """ :type root: TreeNode :type sumt: int :rtype: bool """ if not root and sum: return False
阅读全文
摘要:方法一:自底向上 class Solution(object): # 法一:自底向上 def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ return self.helper(root) >= 0 def hel
阅读全文
摘要:Python3代码 class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: if not nums: return None mid = len(nums) // 2 root = TreeNode(nums[
阅读全文
摘要:class Solution(object): def levelOrderBottom(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if not root: return [] level_queue = []
阅读全文
摘要:class Solution(object): def buildTree(self, inorder, postorder): """ :type inorder: List[int] :type postorder: List[int] :rtype: TreeNode """ # 用字典存储中
阅读全文
摘要:class Solution(object): def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: List[int] :rtype: TreeNode """ self.prein
阅读全文
摘要:# Definition for a binary tree node. class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(obj
阅读全文
摘要:# Definition for a binary tree node. class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(obj
阅读全文
摘要:方法一:广搜,迭代 class Solution(object): # 相当于广度优先搜索,使用队列实现。 # 队列初始化,将根节点压入队列。 # 当队列不为空,进行如下操作: # 弹出一个节点,访问,若左子节点或右子节点不为空,将其压入队列。 def levelOrder(self, root):
阅读全文
摘要:class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def isValidBST(self, root): """
阅读全文
摘要:方法一:二分查找。 class Solution(object): # 二分法 def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ i, j = 0, len(nums) - 1 while i <= j:
阅读全文
摘要:class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True # 左、右子树深度 heightLeft = self.tre
阅读全文
摘要:思路:递归。 终止条件是两个节点都为空,return True; 或者两个节点中有一个为空,return False; 或者两个节点的值不相等,return False; class Solution(object): def isSymmetric(self, root): """ :type r
阅读全文
摘要:class Solution(object): def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ # 根节点值不同,树不同 if p.val != q.val: return Fa
阅读全文
摘要:方法一:二分查找。 class Solution(object): # 二分法 def countNegatives(self, grid): """ :type grid: List[List[int]] :rtype: int """ ans = 0 for nums in grid: if n
阅读全文
摘要:class Solution(object): def kWeakestRows(self, mat, k): """ :type mat: List[List[int]] :type k: int :rtype: List[int] """ power = [sum(line) for line
阅读全文
摘要:class Solution(object): def maxDepthAfterSplit(self, seq): """ :type seq: str :rtype: List[int] """ res = [] if not seq: return res depth, max_depth =
阅读全文