自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(27)
  • 收藏
  • 关注

原创 [笔记] python多重继承以及super()用法

super()super的完整形式是super(class, instance),其中instance是class的实例。多重继承对于定义的每一个类,Python 会计算出一个方法解析顺序(Method Resolution Order, MRO)列表,它代表了类继承的顺序。当使用super(class, instance)时,python会在instance的MRO列表中搜索class的下一个类。使用多重继承时,容易引发冲突。看下面的例子`class Employee: de..

2021-05-27 04:51:33 578

原创 如何解决Project files cannot be watched (are they under network mount)

在第一次使用Pycharm时遇到了如下warning:External file changes sync may be slow Project files cannot be watched (are they under network mount?)找了网上资源,发现有两种解决办法:Add -Didea.filewatcher.disabled=true in Help | Edit Custom VM Options and restart the IDE.该办法亲测可以解决warning

2021-05-25 05:25:34 2567

原创 [笔记] linux全局和个人配置文件

bash_profile和.bashrc的区别/etc 和~(/Users/fengzhu) folder下文件的区别

2021-05-24 23:27:28 355

原创 [笔记]环境变量PATH

这里写自定义目录标题环境变量举例:PATH的使用功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入环境变量举例:PATH的使用我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增

2021-05-24 21:23:10 622

原创 [Leetcode] 958. Check Completeness of a Binary Tree

方法1get the index of each node from 1 to n, if it’s completed binary tree, the len(nodes) == index of last node, else, the count of nodes < index of last nodeclass Solution: def isCompleteTree(self, root: TreeNode) -> bool: queue = [(roo

2021-01-01 12:36:58 55

原创 [Leetcode] 823. Binary Trees With Factors

方法1Dynamic Programmingdp[k] += dp[i] * dp[j], if arr[i] * arr[j] == arr[k]initialize all dp[k] = 1we need to use hashmap or two pointers to get possible arr[i] and arr[j], otherwise the total time complexity is O(N**3), exceed time limittime complexit

2021-01-01 12:24:34 90

原创 [Leetcode] 655. Print Binary Tree

方法1BFS: get level traverse from BFSget the rule of node position in each levelfrom collections import deque# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val#

2021-01-01 12:04:46 79

原创 [Leetcode] 919. Complete Binary Tree Inserter

方法1Deque: use deque to save the no-completed node, if a node become completed, remove it from dequefrom collections import deque# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self

2021-01-01 11:49:29 59

原创 [Leetcode] 366. Find Leaves of Binary Tree

方法1DFS: set all leaf indexes as 1, use dfs to get indexes for each node. use dict to store the index -> nodes. get leaves from by indexes.time complexity: the time for get indexes is O(n)space complexity: O(n)class Solution: def findLeaves(self,

2021-01-01 11:01:35 180

原创 [Leetcode] 1145. Binary Tree Coloring Game

方法1dfs: The best strategy is to select the neighbors of Node(x). if player2 selects Node(x).left, then all nodes in the left subtree will be blue; if player2 selects Node(x).right, then all nodes in the right subtree will be blue; if player2 selects node(

2021-01-01 00:22:52 95

原创 [Leetcode] 669. Trim a Binary Search Tree

方法1经典divide-and-conquer,不用考虑细节,直接dfs left, dfs right, 只需要考虑root和left / right的关系class Solution: def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: if not root: return None if root.val > high:

2020-12-28 23:07:10 46

原创 [Leetcode] 951. Flip Equivalent Binary Trees

方法1DFSclass Solution: def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool: if not root1 and not root2: return True if not root1: return False if not root2: return False

2020-12-28 23:02:25 70

原创 [Leetcode] 993. Cousins in Binary Tree

方法1KEY POINT: if the index of root is n, then index of root.left is 2 * n, index of root.right is 2 * n + 1BFSuse dict to store the node level and node index, check if they are in the same level and not siblingsTime complexity: O(n)Space complexity: O

2020-12-28 22:52:10 86

原创 [Leetcode] 1382. Balance a Binary Search Tree

方法1step1: get in-order traversalstep2: use dfs to convert in-order traversal to BSTtime complexity: O(n)sapce complexity: O(n)class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: in_order = [] self.traverse(root, in

2020-12-28 22:44:22 91

原创 [Leetcode] 968. Binary Tree Cameras

方法1DFS: divide-and-conquer, analyze current values from left and right nodesthere are three return values: camera numbers when(1) All the nodes below this node are covered, but not this node.(2) All the nodes below and including this node are covered,

2020-12-28 22:39:52 67

原创 [Leetcode] 94. Binary Tree Inorder Traversal

方法1recursive traverseTime complexity: O(n)Space complexity: O(n)class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res = [] self.dfs(root, res) return res def dfs(self, root, res): i

2020-12-28 22:15:15 54

原创 [Leetcode] 257. Binary Tree Paths

方法1TraverseTime complexity: O(n)Space complexity: O(n)class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: if root is None: return [] res = [] self.dfs(root, [str(root.val)], res) re

2020-12-28 21:36:54 73

原创 [Leetcode] 742. Closest Leaf in a Binary Tree

方法1Convert binary tree to graph, use BFS to find the closest leaf.Step1: use BFS to go through the tree and store the neighbors of each node (node.left, node.right, parent of node)Step2: get valid nodes with node.val == k.Step3: BFS to get the closest

2020-12-28 21:33:12 62

原创 [Leetcode] 99. Recover Binary Search Tree

最优解:Morris 算法Morris 算法介绍及代码: https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/Use Morris in-order traverse, keep the two wrong nodes and switch their values after the traverse.Time complexity: O(n)Space complexit

2020-12-24 22:47:09 67

原创 [Leetcode] 662. Maximum Width of Binary Tree

方法1:BFSthis is a problem related to BFS. Key point is that:If the index of a parent node is Ci, accordingly we can define the index of its left child node as 2 * Ci, and the index of its right child node as 2 * Ci + 1.# Definition for a binary tree node

2020-12-24 22:23:34 60

原创 [Leetcode] 617. Merge Two Binary Trees 合并两个二叉树

方法1: DFS(divide-and-conquer)there’s no change on the initial nodes t1 and t2.# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self

2020-12-24 22:15:51 70

原创 [Leetcode] 173. Binary Search Tree Iterator

# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass BSTIterator: def __init__(self, root: TreeNode):

2020-12-24 21:40:33 89 1

原创 [Leetcode] 979. Distribute Coins in Binary Tree

方法1: DFSdivide-and-conquer: by dfs, all nodes of a subtree have val of 1, except that the root node of the subtree may have different values. The dfs process will deal with root node in left subtree and root node in right subtree, to make the substree roo

2020-12-24 21:32:23 60

原创 [Leetcode] 226. Invert Binary Tree翻转二叉树

方法1:recursiveclass Solution: def invertTree(self, root: TreeNode) -> TreeNode: if root is None: return None left = self.invertTree(root.left) right = self.invertTree(root.right) root.left = right

2020-12-23 10:42:58 97

原创 [Leetcode] 545. Boundary of Binary Tree 二叉树的边界

思路There are three parts in the result, left boundary, right boundary, and leaf nodes.left boundary and right boundary can use a while loop from root till the leaf.For leaf nodes, we can not use common level order traverse, because leaf nodes may appear

2020-12-23 10:30:35 87

原创 [Leetcode] 543. Diameter of Binary Tree 二叉树的直径

思路test cases: the path with the longest length may go through the root, or it may be in left subtree or right subtree without going through the rootbased on the test cases, we need to check the diameter of each node we meettime complexity: O(n), space

2020-12-23 07:47:57 81

原创 [Leetcode] 98. Validate Binary Search Tree 验证二叉搜索树

[Leetcode] 98. Validate Binary Search Tree 验证二叉搜索树方法1: Traverse方法2:divide-and-conquer方法1: Traversecorner case: what if there are equal numbers;in-order traverse. we need to keep a lastNode, which compares to current node, if lastNode != None and lastNo

2020-12-22 21:08:52 65

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除