随笔分类 - 

摘要:代码一 class Solution(object): # 思路:用前序式DFS,从root到叶子。 def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return 阅读全文
posted @ 2020-09-30 15:25 人间烟火地三鲜 阅读(125) 评论(0) 推荐(0)
摘要:class Solution(object): def minReorder(self, n, connections): """ :type n: int :type connections: List[List[int]] :rtype: int """ # 记录所有点的出、入度:0表示入度,1 阅读全文
posted @ 2020-09-30 15:21 人间烟火地三鲜 阅读(157) 评论(0) 推荐(0)
摘要:class Solution(object): # 思路:BST的中序遍历应该是一个有序序列 def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True midList 阅读全文
posted @ 2020-09-30 15:17 人间烟火地三鲜 阅读(131) 评论(0) 推荐(0)
摘要:class Solution(object): def findSecondMinimumValue(self, root): """ :type root: TreeNode :rtype: int """ if not root: return -1 temp = [] stack = [roo 阅读全文
posted @ 2020-09-30 15:15 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:class Solution(object): def isUnivalTree(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.dfs(root) def dfs 阅读全文
posted @ 2020-09-30 15:13 人间烟火地三鲜 阅读(76) 评论(0) 推荐(0)
摘要:class Solution(object): # 思路: # 先把句子按空格分成单词; # 遍历每个单词中的字符,查看是否是词根,是则替换并遍历下一个单词。 def replaceWords(self, dictionary, sentence): """ :type dictionary: Li 阅读全文
posted @ 2020-09-30 15:11 人间烟火地三鲜 阅读(142) 评论(0) 推荐(0)
摘要:代码一 用list或者字符串方式返回二叉树的所有路径:https://www.cnblogs.com/panweiwei/p/13752895.html class Solution(object): def pathSum(self, root, sum): """ :type root: Tre 阅读全文
posted @ 2020-09-30 14:57 人间烟火地三鲜 阅读(146) 评论(0) 推荐(0)
摘要:一、用list返回二叉树的所有路径 class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def findAllPa 阅读全文
posted @ 2020-09-30 08:55 人间烟火地三鲜 阅读(291) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS模板,百试不爽。 def listOfDepth(self, tree): """ 阅读全文
posted @ 2020-09-13 16:22 人间烟火地三鲜 阅读(107) 评论(0) 推荐(0)
摘要:class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return None # 二分法找nums中点 start, 阅读全文
posted @ 2020-09-13 16:19 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True # 求左子树深度 leftDenth = self.treeD 阅读全文
posted @ 2020-09-13 16:17 人间烟火地三鲜 阅读(97) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路:层序模板。 def maxDepth(self, root): """ :type 阅读全文
posted @ 2020-09-13 16:16 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def kthLargest(self, root, k): """ :type 阅读全文
posted @ 2020-09-13 16:13 人间烟火地三鲜 阅读(109) 评论(0) 推荐(0)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 层序模板,加个标记用来判断temp的正反序即可。 def levelOrder(self, roo 阅读全文
posted @ 2020-09-13 16:11 人间烟火地三鲜 阅读(121) 评论(0) 推荐(1)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路: # 层序遍历,套用模板,百试不爽。 def levelOrder(self, root): 阅读全文
posted @ 2020-09-13 16:09 人间烟火地三鲜 阅读(74) 评论(0) 推荐(0)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def levelOrder(self, root): """ :type root: TreeNod 阅读全文
posted @ 2020-09-13 16:07 人间烟火地三鲜 阅读(75) 评论(0) 推荐(0)
摘要:class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.helper(root.left, 阅读全文
posted @ 2020-09-13 16:05 人间烟火地三鲜 阅读(96) 评论(0) 推荐(0)
摘要:class Solution(object): def isCousins(self, root, x, y): """ :type root: TreeNode :type x: int :type y: int :rtype: bool """ # 用字典记录每个节点的父节点、以及该节点的深度( 阅读全文
posted @ 2020-09-13 16:01 人间烟火地三鲜 阅读(119) 评论(0) 推荐(0)
摘要:DFS应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def increasingBST(self, root): """ :type root: TreeNod 阅读全文
posted @ 2020-09-13 15:57 人间烟火地三鲜 阅读(129) 评论(0) 推荐(0)
摘要:class Solution(object): def leafSimilar(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ res1 = [] self.dfs(root1 阅读全文
posted @ 2020-09-13 15:54 人间烟火地三鲜 阅读(128) 评论(0) 推荐(0)