随笔分类 - LeetCode
刷过的题记录一下,思路仅供参考,并非最优解。
摘要: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 注:"/"符号两边只要有一个float类型,结果自动取float类型!!! class Solution(object): def averageOf
阅读全文
摘要:方法一:递归。 class Solution(object): # 递归 def mergeTrees(self, t1, t2): """ :type t1: TreeNode :type t2: TreeNode :rtype: TreeNode """ if not t1 and not t2
阅读全文
摘要:方法一: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): def largestValues(self, root): """ :type root: Tree
阅读全文
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def findBottomLeftValue(self, root): """
阅读全文
摘要:类似题目参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 先特判: # 当节点root为空时:return None; # 当p或q的值域等于root时,return root; # 再递归判
阅读全文
摘要:方法一: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
阅读全文