Recursion and Trees
74 / 170Recursion and TreesLesson 7 of 13About 73 min
A queue instead of the call stack, and the one line that turns a flat walk into levels.
Three readings of the same loop. level_sums returns the sum of each level. widest_level returns the number of nodes on the widest level. leftmost_per_level returns the first value on each level.
Tests
tree = T(1, T(2, T(4), T(5)), T(3))print(level_sums(tree), level_sums(None))print(widest_level(tree), widest_level(None), widest_level(T(9)))print(leftmost_per_level(tree), leftmost_per_level(None))Output
Run tests to see output.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.