Path¶
Path¶
112. Path Sum (Easy)¶
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def recursion(root, required):
if root:
if not root.left and not root.right and root.val == required:
return True
a = recursion(root.left, required - root.val)
b = recursion(root.right, required - root.val)
return a or b
return recursion(root, targetSum)
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
stack = [(root, targetSum)]
while stack:
node, required = stack.pop()
if node:
if not node.left and not node.right and node.val == required:
return True
stack.append((node.right, required - node.val))
stack.append((node.left, required - node.val))
return False
257. Binary Tree Paths (Easy)¶
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
def recursion(node):
nonlocal path, paths
if node:
path.append(str(node.val))
if not node.left and not node.right:
paths.append("->".join(path))
recursion(node.left) if node.left else None
recursion(node.right) if node.right else None
path.pop()
path = []
paths = []
recursion(root)
return paths
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
stack = [(root, False)]
path = []
paths = []
while stack:
node, visited = stack.pop()
if node:
if visited:
path.pop()
else:
path.append(str(node.val))
if not node.left and not node.right:
paths.append("->".join(path))
stack.append((node, True))
stack.append((node.right, False)) if node.right else None
stack.append((node.left, False)) if node.left else None
return paths
113. Path Sum II (Medium)¶
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
def recursion(node):
nonlocal path, path_sum, paths
if node:
path.append(node.val)
path_sum += node.val
if not node.left and not node.right and path_sum == targetSum:
paths.append(deepcopy(path))
recursion(node.left) if node.left else None
recursion(node.right) if node.right else None
path.pop()
path_sum -= node.val
path_sum = 0
path = []
paths = []
recursion(root)
return paths
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
stack = [(root, False)]
path = []
paths = []
path_sum = 0
while stack:
node, visited = stack.pop()
if node:
if visited:
path.pop()
path_sum -= node.val
else:
path.append(node.val)
path_sum += node.val
if not node.left and not node.right and path_sum == targetSum:
paths.append(deepcopy(path))
stack.append((node, True))
stack.append((node.right, False)) if node.right else None
stack.append((node.left, False)) if node.left else None
return paths
Math in it: Bitwise, Divmod¶
1457. Pseudo-Palindromic Paths in a Binary Tree (Medium)¶
Solution
Refer solution for 266. Palindrome Permutation (Easy).
def pseudoPalindromicPaths(self, root: Optional[TreeNode]) -> int:
def recursion(node, path):
nonlocal ans
if node:
path = (path) ^ (1 << node.val)
if not node.left and not node.right:
ans += path & (path - 1) == 0
recursion(node.left, path) if node.left else None
recursion(node.right, path) if node.right else None
ans = 0
recursion(root, 0)
return ans
def pseudoPalindromicPaths(self, root: Optional[TreeNode]) -> int:
stack = [(root, 0)]
ans = 0
while stack:
node, path = stack.pop()
if node:
path = (path) ^ (1 << node.val)
if not node.left and not node.right:
ans += path & (path - 1) == 0
stack.append((node.right, path))
stack.append((node.left, path))
return ans
1022. Sum of Root To Leaf Binary Numbers (Easy)¶
def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
def recursion(node):
nonlocal total, cur_binary
if node:
cur_binary = 2 * cur_binary + node.val
if not node.left and not node.right:
total += cur_binary
recursion(node.left)
recursion(node.right)
cur_binary //= 2
cur_binary = total = 0
recursion(root)
return total
def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
stack = [(root, 0)]
total = 0
while stack:
node, cur_binary = stack.pop()
if node:
cur_binary = cur_binary * 2 + node.val
if not node.left and not node.right:
total += cur_binary
stack.append((node.right, cur_binary))
stack.append((node.left, cur_binary))
return total
def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
q = [(root, 0)]
total = 0
while q:
mini_q = []
while q:
node, cur_binary = q.pop(0)
if node:
cur_binary = cur_binary * 2 + node.val
if not node.left and not node.right:
total += cur_binary
mini_q.append((node.left, cur_binary))
mini_q.append((node.right, cur_binary))
q = mini_q
return total
129. Sum Root to Leaf Numbers (Medium)¶
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def recursion(node):
nonlocal total, cur_decimal
if node:
cur_decimal = 10 * cur_decimal + node.val
if not node.left and not node.right:
total += cur_decimal
recursion(node.left)
recursion(node.right)
cur_decimal //= 10
cur_decimal = total = 0
recursion(root)
return total
def sumNumbers(self, root: Optional[TreeNode]) -> int:
stack = [(root, 0)]
total = 0
while stack:
node, cur_decimal = stack.pop()
if node:
cur_decimal = cur_decimal * 10 + node.val
if not node.left and not node.right:
total += cur_decimal
stack.append((node.right, cur_decimal))
stack.append((node.left, cur_decimal))
return total
def sumNumbers(self, root: Optional[TreeNode]) -> int:
q = [(root, 0)]
total = 0
while q:
mini_q = []
while q:
node, cur_decimal = q.pop(0)
if node:
cur_decimal = cur_decimal * 10 + node.val
if not node.left and not node.right:
total += cur_decimal
mini_q.append((node.left, cur_decimal))
mini_q.append((node.right, cur_decimal))
q = mini_q
return total
Simple Path Tracking¶
404. Sum of Left Leaves (Easy)¶
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def recursion(node):
nonlocal total
if node:
if node.left and not node.left.left and not node.left.right:
total += node.left.val
recursion(node.left)
recursion(node.right)
total = 0
recursion(root)
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
stack = [root]
total = 0
while stack:
node = stack.pop()
if node:
if node.left and not node.left.left and not node.left.right:
total += node.left.val
stack.append(node.right)
stack.append(node.left)
return total
1469. Find All The Lonely Nodes (Easy)¶
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
def recursion(root):
if root:
if root.left and root.right:
...
elif root.left or root.right:
lone_node = root.left or root.right
lone_wolves.append(lone_node.val)
recursion(root.left)
recursion(root.right)
lone_wolves = []
recursion(root)
return lone_wolve
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
stack = [root]
lone_wolves = []
while stack:
node = stack.pop()
if node:
if node.left and node.right:
...
elif node.left or node.right:
lone_node = node.left or node.right
lone_wolves.append(lone_node.val)
stack.append(node.right) if node.right else None
stack.append(node.left) if node.left else None
return lone_wolves
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
"""BFS"""
q = [root]
lone_wolves = []
while q:
mini_q = []
while q:
node = q.pop(0)
if node:
if node.left and node.right:
...
elif node.left or node.right:
lone_node = node.left or node.right
lone_wolves.append(lone_node.val)
mini_q.append(node.left) if node.left else None
mini_q.append(node.right) if node.right else None
q = mini_q
return lone_wolves