Problem Introduction: https://www.youtube.com/watch?v=zIkDfgFAg60
Recursive Solution Template:
1 | <> res = 0; |
129. Sum Root to Leaf Numbers
[Recursive DFS Solution] Set a helper recursion function that, accepts two inputs: root
and prevValue
. The prevVale would track the parent’s nodes accumulative sum by root.val + prevValue * 10
, and pass as input to children nodes.
If the recursion reach the leaf nodes, add the sum to the global result and return. Enter the next recursion.
1 | class Solution { |
257. Binary Tree Paths
[Solution] Construct a recursive helper function that take path as input.
1 | class Solution { |
113. Path Sum II
[Solution]
1 | class Solution { |
437. Path Sum III
Leetcode: https://leetcode.com/problems/path-sum-iii/
Solution: Use a dictionary to store accumalative sum to this node
, key is the sum
, and value is the count
of the sum. At each node, test if curSum - sum is in dict
, if so, add count to global count.
1 | var pathSum = function(root, sum) { |
494. Target Sum
Leetcode: https://leetcode.com/problems/target-sum/
Solution: Similar to above path sum, use a dictionary to remember the count of previous sums. If in the current layer there exist sum == target
, then add the count of prev sum to global counter.
1 | def findTargetSumWays(self, nums: List[int], S: int) -> int: |
Path through root
687. Longest Univalue Path
Leetcode: https://leetcode.com/problems/longest-univalue-path/description/
Solution: https://www.youtube.com/watch?v=asihnVxQuL4
[Solution]
Use a global variable len
to keep track of the maximum length of the nodes, which would be the sum(max(leftNode), max(rightNode)).
Since the path might go through the root, if so, we need to pass to the root max(left, right) + 1
when returning, rather than max(left + right) + 1
If the path is disconnected, we set return value to 0.
1 | class Solution { |
124. Binary Tree Maximum Path Sum
Leetcode: https://leetcode.com/problems/binary-tree-maximum-path-sum/description/
[Solution]
- Global variable
max
to keep the answer - When reach to the leaf node, update
max = leaf.val + 0 + 0
- When pass the value to upper level, use
Math.max(left, right) + node.val
to pass either left or right path to the upper recursion.
1 | // Solution in discussion(cleaner) |
1 | def maxPathSum(self, root: TreeNode) -> int: |
543. Diameter of Binary Tree
Leetcode: https://leetcode.com/problems/diameter-of-binary-tree/description/
Compute the longest path between any nodes in a tree.
[Solution]
- Use a global variable
maxLen
- Update
maxLen
in every recursion, compute the sum of left and right nodes + 1 as current max length - When returning value to the uppper recusion, only return either right or left max length
1 | class Solution { |
1 | class Solution: |
337. House Robber III
Leetcode: https://leetcode.com/problems/house-robber-iii/
Video: https://www.youtube.com/watch?v=-i2BFAU25Zk
1 | var rob = function(root) { |
863. All Nodes Distance K in Binary Tree
Leetcode: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
Note:
- Do a dfs to form an undirected graph from all nodes first
- Then do a bfs to find the nodes in certain distance
1 | def distanceK(self, root, target, K): |