[LeetCode] questions conclustion_Path in Tree

时间:2023-03-10 06:17:33
[LeetCode] questions conclustion_Path in Tree

Path in Tree:

[LeetCode] 112. Path Sum_Easy tag: DFS       input: root, target,   return True if exists sum(root-> leaf) == target else False       1

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS        input: root,   return all paths from root to leaves

[LeetCode] 113. Path Sum II      input: root, target, return all paths such that sum(root-> leaf) == target else []

[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer   input: root, return max path sum such that any node -> any node.

[LeetCode] 437. Path Sum III_ Easy tag: DFS input: root, return all paths s.t a path sum == target from any node -> any child node .

[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive  input:root, return max number of nodes s.t  a path any node -> any node and all nodes have same value,

[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive  input: root, return max number of nodes s.t a path any node -> child node and nodes are increasing consecutive order.      1

[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive  input: root, return max number of nodes s.t a path any node -> any node and nodes are increasing or decreasing consecutive order.

when it becomes path from any node -> any node

we define a helper function.

def helper(root):
if not root: return 0
l, r = helper(root.left), helper(root.right)
l = l if l and condition else 0
# root.val == root.left.val LC. 687
# root.val + 1 == root.left.val LC. 298
r = r if r and condition else 0
self.ans = max(self.ans, possible values)
return max(possible values) # remember that path return should not be left and right both, only choose one.