def cloneTree(root: TreeNode) -> TreeNode: if root: newRoot = TreeNode(root.val) newRoot.left = cloneTree(root.left) newRoot.right = cloneTree(root.right) return newRoot return None
def convertBSTImpl(root: TreeNode, v: int) -> (TreeNode, int): if not root: return None, 0 newRight, sumRight = convertBSTImpl(root.right, v) newRoot = TreeNode(root.val + sumRight + v) newLeft, sumLeft = convertBSTImpl(root.left, newRoot.val) newRoot.left = newLeft newRoot.right = newRight return newRoot, sumLeft + root.val + sumRight
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: if not root: return None if root.val > high: return self.trimBST(root.left, low, high) if root.val < low: return self.trimBST(root.right, low, high) root.left = self.trimBST(root.left, low, high) root.right = self.trimBST(root.right, low, high) return root
def helper(beg: int, end: int) -> TreeNode: if beg > end: return None mid = beg + (end - beg) // 2 left = helper(beg, mid - 1) root = TreeNode(self.head.val) self.head = self.head.next root.left = left root.right = helper(mid + 1, end) return root
def impl(pre_b: int, pre_e: int, in_b: int, in_e: int) -> TreeNode: if pre_b == pre_e: return None val = preorder[pre_b] root = TreeNode(val) m = inorder_index[val] left_end = pre_b + 1 + m - in_b root.left = impl(pre_b + 1, left_end, in_b, m) root.right = impl(left_end, pre_e, m + 1, in_e) return root
def impl(root: TreeNode) -> TreeNode: if not root: return None left_end = impl(root.left) right_end = impl(root.right) right = root.right root.right = root.left root.left = None if left_end: left_end.right = right else: root.right = right if right_end: return right_end if left_end: return left_end return root
Runtime: 40 ms, faster than 89.58% of Python3 online submissions for Path Sum. Memory Usage: 15.4 MB, less than 98.89% of Python3 online submissions for Path Sum. """ class Solution: def hasPathSum(self, root: TreeNode, sum: int) -> bool: if root is None: return False if root.left is None and root.right is None: return sum == root.val if self.hasPathSum(root.left, sum - root.val): return True return self.hasPathSum(root.right, sum - root.val) t1 = TreeNode(5) t1.left = TreeNode(4) t1.right = TreeNode(8) t1.left.left = TreeNode(11) t1.left.left.left = TreeNode(7) t1.left.left.right = TreeNode(2) t1.right.left = TreeNode(13) t1.right.right = TreeNode(4) t1.right.right.right = TreeNode(1) tests = [ (t1, 22, True) ]
# Memory Usage: 18.8 MB, less than 5.07% of Python3 online submissions for Convert BST to Greater Tree. from Common.Leetcode import TreeNode from Common.TreeUtils import printTree class Solution: def convertBST(self, root: TreeNode) -> TreeNode: def convertBSTImpl(root: TreeNode, v: int) -> (TreeNode, int): if not root: return None, 0 newRight, sumRight = convertBSTImpl(root.right, v) newRoot = TreeNode(root.val + sumRight + v) newLeft, sumLeft = convertBSTImpl(root.left, newRoot.val) newRoot.left = newLeft newRoot.right = newRight return newRoot, sumLeft + root.val + sumRight return convertBSTImpl(root, 0)[0] root1 = TreeNode(4) root1.left = TreeNode(1) root1.right = TreeNode(6) root1.left.left = TreeNode(0) root1.left.right = TreeNode(2) root1.left.right.right = TreeNode(3) root1.right.left = TreeNode(5) root1.right.right = TreeNode(7) root1.right.right.right = TreeNode(8) printTree(Solution().convertBST(root1))
if L[1] > R[1]: return L[0], L[1] + 1 if R[1] > L[1]: return R[0], R[1] + 1 return root, L[1] + 1 return dfs(root)[0] def printTree(root: TreeNode): def printTreeImpl(root: TreeNode): if root is None: return printTreeImpl(root.left) print(root.val, ' ', end='') printTreeImpl(root.right) printTreeImpl(root) print() root = TreeNode(3) root.left = TreeNode(5) root.left.left = TreeNode(6) root.left.right = TreeNode(2) root.left.right.left = TreeNode(7) root.left.right.right = TreeNode(4) root.right = TreeNode(1) root.right.left = TreeNode(0) root.right.right = TreeNode(8) res = Solution().lcaDeepestLeaves(root) printTree(res)
class Solution: @staticmethod def isLeave(root: TreeNode): return root.left is None and root.right is None def sumOfLeftLeavesImpl(self, root: TreeNode, isLeft: bool) -> int: if Solution.isLeave(root): return root.val if isLeft else 0 sumleft = sumright = 0 if root.left: sumleft = self.sumOfLeftLeavesImpl(root.left, True) if root.right: sumright = self.sumOfLeftLeavesImpl(root.right, False) return sumleft + sumright def sumOfLeftLeaves(self, root: TreeNode) -> int: if root is None: return 0 return self.sumOfLeftLeavesImpl(root, False) t1 = TreeNode(3) t1.left = TreeNode(9) t1.right = TreeNode(20) t1.right.left = TreeNode(15) t1.right.right = TreeNode(7) print(Solution().sumOfLeftLeaves(t1)) # 24
if root is None: return 0 dl = dr = 0 if root.left: dl = self.minDepth(root.left) if root.right: dr = self.minDepth(root.right) if dl > 0 and dr > 0: return 1 + min(dl, dr) if dl > 0: return dl + 1 return dr + 1 tree1 = TreeNode(3) tree1.left = TreeNode(9) tree1.right = TreeNode(20) tree1.right.left = TreeNode(15) tree1.right.right = TreeNode(7) tree2 = TreeNode(1) tree2.left = TreeNode(2) tests = [(tree1, 2), (tree2, 2)] run_functional_tests(Solution().minDepth, tests)
class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: def binaryTreePathsImpl(root: TreeNode, result: List[str], path: str): if root is None: return newpath: str = path if len(newpath) > 0: newpath += "->" newpath += str(root.val) if root.left is None and root.right is None: result.append(newpath) else: if root.left is not None: binaryTreePathsImpl(root.left, result, newpath) if root.right is not None: binaryTreePathsImpl(root.right, result, newpath) result: List[str] = [] binaryTreePathsImpl(root, result, "") return result t1 = TreeNode(1) t1.left = TreeNode(2) t1.left.right = TreeNode(5) t1.right = TreeNode(3) tests = [(t1, ['1->2->5', '1->3'])] run_functional_tests(Solution().binaryTreePaths, tests)
self.verticalTraversalImpl(cache, root, 0, 0) result: List[List[int]] = [] for d in sorted(cache): result.append([]) for s in sorted(cache[d]): for v in sorted(cache[d][s]): result[-1].append(v) return result # [0,2,1,3,null,null,null,4,5,null,7,6,null,10,8,11,9] root1 = TreeNode(3) root1.left = TreeNode(9) root1.right = TreeNode(20) root1.right.left = TreeNode(15) root1.right.right = TreeNode(7) root2 = TreeNode(1) root2.left = TreeNode(2) root2.left.left = TreeNode(4) root2.left.right = TreeNode(5) root2.right = TreeNode(3) root2.right.left = TreeNode(6) root2.right.right = TreeNode(7) tests = [ (root1, [[9],[3,15],[20],[7]]), (root2, [[4],[2],[1,5,6],[3],[7]])
if not current.left and not current.right: if len(nodes) <= 1: result += 1 dfs(current.left) dfs(current.right) if current.val in nodes: nodes.remove(current.val) else: nodes.append(current.val) dfs(root) return result root1 = TreeNode(2) root1.left = TreeNode(3) root1.left.left = TreeNode(3) root1.left.right = TreeNode(1) root1.right = TreeNode(1) root1.right.right = TreeNode(1) # if Solution().pseudoPalindromicPaths(root1) == 2: # print("PASS") # else: # print("FAIL") root2 = TreeNode(2) root2.left = TreeNode(1) root2.left.left = TreeNode(1) root2.left.right = TreeNode(3) root2.left.right.right = TreeNode(1)