algorithm
朱枫
这个作者很懒,什么都没留下…
展开
-
[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 · 51 阅读 · 0 评论 -
[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 · 85 阅读 · 0 评论 -
[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 · 74 阅读 · 0 评论 -
[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 · 157 阅读 · 0 评论 -
[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 · 92 阅读 · 0 评论 -
[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 · 43 阅读 · 0 评论 -
[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 · 60 阅读 · 0 评论 -
[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 · 70 阅读 · 0 评论 -
[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 · 86 阅读 · 0 评论 -
[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 · 63 阅读 · 0 评论 -
[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 · 38 阅读 · 0 评论 -
[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 · 71 阅读 · 0 评论 -
[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 · 55 阅读 · 0 评论 -
[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 · 61 阅读 · 0 评论 -
[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 · 56 阅读 · 0 评论 -
[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 · 65 阅读 · 0 评论 -
[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 · 81 阅读 · 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 · 56 阅读 · 0 评论 -
[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 · 79 阅读 · 0 评论 -
[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 · 81 阅读 · 0 评论 -
[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 · 78 阅读 · 0 评论 -
[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 · 61 阅读 · 0 评论