Created
January 13, 2018 23:25
Analysis of sum from root to leaf node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Problem statement: | |
Find minimum sum for each path from root node to leaf node in a tree. | |
0 | |
/ | \ | |
5 3 6 | |
/ /\ /\ | |
4 2 0 1 5 | |
/ / | |
1 10 | |
\ | |
1 | |
Julia's analysis: January 13, 2018 | |
Go over all the paths | |
0->5->4 9 | |
0->3->2->1->1 7 | |
0->3->0->10 13 | |
0->6->1 7 | |
0->6->5 11 | |
Find minimum value | |
total 5 paths, not path itself, need minimum sales path -> Math.min(9, 7, 13, 7, 11) -> minimum 7 | |
DFS search: | |
depth first search -> recursive | |
Template: | |
if the node is null | |
return 0 | |
base case | |
if there is no child | |
return node.value | |
// have multiple child | |
minPath = Int.Max | |
foreach child | |
fidnMinPath | |
update minPath | |
return node.value + minPath | |
recurrence formula | |
Write code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment