随笔分类 - 树
摘要:代码一 class Solution(object): # 思路:用前序式DFS,从root到叶子。 def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return
阅读全文
摘要:class Solution(object): def minReorder(self, n, connections): """ :type n: int :type connections: List[List[int]] :rtype: int """ # 记录所有点的出、入度:0表示入度,1
阅读全文
摘要:class Solution(object): # 思路:BST的中序遍历应该是一个有序序列 def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True midList
阅读全文
摘要:class Solution(object): def findSecondMinimumValue(self, root): """ :type root: TreeNode :rtype: int """ if not root: return -1 temp = [] stack = [roo
阅读全文
摘要:class Solution(object): def isUnivalTree(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.dfs(root) def dfs
阅读全文
摘要:class Solution(object): # 思路: # 先把句子按空格分成单词; # 遍历每个单词中的字符,查看是否是词根,是则替换并遍历下一个单词。 def replaceWords(self, dictionary, sentence): """ :type dictionary: Li
阅读全文
摘要:代码一 用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): # BFS模板,百试不爽。 def listOfDepth(self, tree): """
阅读全文
摘要:class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return None # 二分法找nums中点 start,
阅读全文
摘要:class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True # 求左子树深度 leftDenth = self.treeD
阅读全文
摘要:方法一: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
阅读全文
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 层序模板,加个标记用来判断temp的正反序即可。 def levelOrder(self, roo
阅读全文
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路: # 层序遍历,套用模板,百试不爽。 def levelOrder(self, root):
阅读全文
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def levelOrder(self, root): """ :type root: TreeNod
阅读全文
摘要:class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.helper(root.left,
阅读全文
摘要: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
阅读全文