But it could never look like this:
Every level must be filled left to right no matter what. This implementation is interesting because the only information you have about each node is it's index in the list. This means that in order to find its parent or child nodes you need a formula that takes the nodes index and outputs the index of the parent/child. I was able to find this formula by writing out the the first 4 or 5 cases and looking for a pattern. For example, the index of the left child 'j' of a node at index 'i':
i j
0 -> 1
1 -> 3
2 -> 5
3 -> 7
Then using this data I tried to find a pattern. In this case j = (2*i)+1, so the index of every left child of a node at index 'i' is equal to (2*i)+1. After finding the patterns for navigating the index's for parent and child nodes you could put them into methods and basically use the same techniques for solving inorder, max_sum, etc.
No comments:
Post a Comment