def pruneTree(self, root: TreeNode) -> TreeNode: if root: root.left = self.pruneTree(root.left) root.right = self.pruneTree(root.right) if root.val or root.left or root.right: # The root node could not be pruned any longer. return root
def sortedArrayToBST(self, nums: List[int]) -> TreeNode: if not nums: # Empty tree. return None m = (len(nums) - 1) // 2 root = TreeNode(nums[m]) root.left = self.sortedArrayToBST(nums[:m]) root.right = self.sortedArrayToBST(nums[m + 1:]) return root
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: if not root: return TreeNode(val) if val < root.val: root.left = self.insertIntoBST(root.left, val) else: root.right = self.insertIntoBST(root.right, val) return root
def do() -> TreeNode: currVal = next(vals) if currVal == '#': return None root = TreeNode(int(currVal)) root.left = do() root.right = do() return root
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode: if not t1: return t2 if not t2: return t1 t1.val += t2.val t1.left = self.mergeTrees(t1.left, t2.left) t1.right = self.mergeTrees(t1.right, t2.right) return t1
def g(first: int, last: int) -> List[TreeNode]: rslt = [] for root in range(first, last + 1): for leftNode in g(first, root - 1): for rightNode in g(root + 1, last): currNode = TreeNode(root) currNode.left = leftNode currNode.right = rightNode rslt.append(currNode) return rslt or [None] # Need to return [None] when rslt is empty.
def do(start: int, end: int) -> TreeNode: if end < start: return None maxIdx, maxNum = start, nums[start] for i in range(start + 1, end + 1): if nums[i] > maxNum: maxIdx, maxNum = i, nums[i] root = TreeNode(maxNum) root.left = do(start, maxIdx - 1) root.right = do(maxIdx + 1, end) return root
def check(n: int) -> list[TreeNode]: if n not in memo: rslt = [] for i in range(n): j = n - 1 - i for left in check(i): for right in check(j): root = TreeNode(0) root.left = left root.right = right rslt.append(root) memo[n] = rslt return memo[n]
def deleteNode(self, root: TreeNode, key: int) -> TreeNode: if not root: return None if root.val < key: root.right = self.deleteNode(root.right, key) elif root.val > key: root.left = self.deleteNode(root.left, key) else: # Root is the target node to be deleted. if not root.left: return root.right elif not root.right: return root.left else: # Update the root value with the minimum value on its right # sub tree, then delete the old minimum tree node. curr = root.right while curr.left: curr = curr.left root.val = curr.val root.right = self.deleteNode(root.right, root.val) return root
def create_tree(self, l: int, r: int) -> TreeNode: if l > r: return None m = (l + r) // 2 left = self.create_tree(l, m - 1) root = TreeNode(self.currNodeInList.val) root.left = left self.currNodeInList = self.currNodeInList.next root.right = self.create_tree(m + 1, r) return root
def do(start: int, end: int) -> TreeNode: if start > end: return None if start == end: return TreeNode(preorder[start]) rootVal = preorder[start] left = start + 1 while left <= end and preorder[left] < rootVal: left += 1 root = TreeNode(rootVal) root.left = do(start + 1, left - 1) root.right = do(left, end) return root
class Solution: def pseudoPalindromicPaths(self, root: TreeNode) -> int: def search(currNode: TreeNode) -> None: if currNode: freqs[currNode.val - 1] += 1 if not (currNode.left or currNode.right): # Reach a leaf node. if sum(f & 1 for f in freqs) <= 1: self.paths += 1 else: search(currNode.left) search(currNode.right) freqs[currNode.val - 1] -= 1 # Restore to search other paths. freqs = [0] * 9 self.paths = 0 search(root) return self.paths r = TreeNode(2) r.left = TreeNode(3) r.right = TreeNode(1) r.left.left = TreeNode(3) r.left.right = TreeNode(1) r.right.right = TreeNode(1) print(Solution().pseudoPalindromicPaths(r))
def check(currNode: TreeNode) -> int: if not currNode: return 0 leftMax, rightMax = check(currNode.left), check(currNode.right) leftSame = rightSame = 0 if currNode.left and currNode.left.val == currNode.val: leftSame = 1 + leftMax if currNode.right and currNode.right.val == currNode.val: rightSame = 1 + rightMax self.rslt = max(self.rslt, leftSame + rightSame) return max(leftSame, rightSame) self.rslt = 0 check(root) return self.rslt root = TreeNode(1) root.right = TreeNode(1) node = root.right node.left = TreeNode(1) node.right = TreeNode(1) node.left.left = TreeNode(1) node.left.right = TreeNode(1) node.right.left = TreeNode(1) print(Solution().longestUnivaluePath(root))