随笔分类 - Python
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def kthSmallest(self, root, k): """ :type
阅读全文
摘要:BFS模板的应用 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def connect(self, root): """ :type root: Node :rtype
阅读全文
摘要:输入: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left&q
阅读全文
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def minDepth(self, root): """ :type root: TreeNode
阅读全文
摘要:class Solution(object): # 题意要转成平衡二叉搜索树 # 先转成list,在转BST呗。 def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ if not head: r
阅读全文
摘要:方法一:递归 class Solution(object): # 递归思路: # 当p,q都在root的右子树中时,递归root.right并返回; # 当p,q都在root的左子树中时,递归root.left并返回; # 返回值:最近公共祖先root。 def lowestCommonAncest
阅读全文
摘要:# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self.next = None # Definition for a binary tree node.
阅读全文
摘要:class Solution(object): def constructMaximumBinaryTree(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return rootval = max(n
阅读全文
摘要:# Definition for a Node. class Node(object): def __init__(self, val=None, children=None): self.val = val self.children = children 方法一:迭代 class Solutio
阅读全文
摘要:方法一:迭代 class Solution(object): # 迭代 def preorder(self, root): """ :type root: Node :rtype: List[int] """ res = [] if not root: return res stack = [roo
阅读全文
摘要:class Solution(object): def pathSum(self, root, target): """ :type root: TreeNode :type sumt: int :rtype: int """ self.count = 0 myDict = {0: 1} def d
阅读全文
摘要:class Solution(object): def levelOrder(self, root): """ :type root: Node :rtype: List[List[int]] """ if not root: return [] ans = [] self.dfs(1, root,
阅读全文
摘要:class Solution(object): def sumOfLeftLeaves(self, root): """ :type root: TreeNode :rtype: int """ if not root or (not root.left and not root.right): r
阅读全文
摘要:类似题:https://www.cnblogs.com/panweiwei/p/13585987.html class Solution(object): def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :ty
阅读全文
摘要:class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return root.left, root.right = root.rig
阅读全文
摘要:class Solution(object): # 递归思路: # (1)如果二叉树为空,节点个数为0 # (2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1 def countNodes(self, root): """ :type root: TreeNode
阅读全文
摘要:方法一:DFS class Solution(object): # 方法一:深搜 def rightSideView(self, root): """ :type root: TreeNode :rtype: List[int] """ ans = [] self.dfs(root, 0, ans)
阅读全文
摘要:class BSTIterator(object): def __init__(self, root): """ :type root: TreeNode """ self.item = [] while root: self.item.append(root) root = root.left d
阅读全文
摘要:方法一:迭代 class Solution(object): # 迭代 def postorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return [] ans = []
阅读全文
摘要:方法一:迭代代码一: class Solution(object): # 迭代 def preorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return [] pre_s
阅读全文