Conversation
kyra-patton
left a comment
There was a problem hiding this comment.
✨💫 Nice work, Ivette. I left a couple comments on how you might refactor, particularly your height implementation. Let me know what questions you have.
🟢
| else: | ||
| parent_node = parent_node.left | ||
| else: | ||
| return "WTF" |
There was a problem hiding this comment.
🤯
You could remove this line and leave line 36 as an else (or as is)
| # Time Complexity: O(log(n)) | ||
| # Space Complexity: O(1) |
| self.root = added_node | ||
| return self.root | ||
| parent_node = self.root | ||
| while True: |
There was a problem hiding this comment.
Generally try to avoid having while loop conditions that can never be Falsey. You always want to be making progress towards make the loop condition False in the loop body.
How might you refactor your code to eliminate the while True. (Hint: look at your find implementation)
| # Space Complexity: | ||
| # Time Complexity: O(log(n)) | ||
| # Space Complexity: O(1) | ||
| def find(self, key): |
| return None | ||
|
|
||
| # left, current, right | ||
| def inorder_helper(self, current_node, node_list = None): |
| pass | ||
| return self.inorder_helper(self.root) | ||
|
|
||
| def preorder_helper(self, current_node, node_list = None): |
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder(self): |
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder_helper(self, current_node, node_list = None): |
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder(self): |
| # Time Complexity: O(nlog(n)) | ||
| # Space Complexity: O(m) where m is the number of end nodes |
There was a problem hiding this comment.
✨ This time and space complexity is correct for your solution!
You could refactor your code to achieve an O(n) time and space solution. Instead of having height_helper return a list, have it return an integer. When you make a recursive call on the left/right subtree, you know that your height is 1 + the height of the deepest subtree. So you can say that the height is 1 + max(height of left subtree, height of right subtree).
No description provided.