File tree 1 file changed +6
-4
lines changed
1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change 1
1
# 437. Path Sum III
2
2
3
3
## Brute Force Top Down Recursive Solution
4
- - Runtime: O(3 ^N)
5
- - Space: O(3 ^N)
4
+ - Runtime: O(2 ^N)
5
+ - Space: O(2 ^N)
6
6
- N = Number of nodes in tree
7
7
8
8
Thinking about the brute force method, for every node in the tree, traverse their left child and right child to find the target sum.
@@ -13,8 +13,10 @@ This is similar to a DFS implementation.
13
13
14
14
The runtime can be calculated in this way.
15
15
For each recursive call, we want to traverse the left and right children down to the leaf nodes, this is O(N) runtime.
16
- Since we would have to do this again but for the left and right children, it about O(N-1) for each.
17
- So we basically have O(N) three times which means O(3^N).
16
+ Since we would have to do this again but for the left and right children, it about O(log(N)) for each.
17
+ So we basically have O(N), O(log(N)), O(log(N)).
18
+ The two O(log(N)) can be simplified to O(N-1).
19
+ So O(N) and O(N-1) per call = O(2^N)
18
20
19
21
Remember the formula for recursion, (Number of calls ^ (Big O per call))
20
22
You can’t perform that action at this time.
0 commit comments