-
Notifications
You must be signed in to change notification settings - Fork 68
Ivette F. - Spruce #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨💫 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯
You could remove this line and leave line 36 as an else (or as is)
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
self.root = added_node | ||
return self.root | ||
parent_node = self.root | ||
while True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(1) | ||
def find(self, key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ Nice iterative solution
return None | ||
|
||
# left, current, right | ||
def inorder_helper(self, current_node, node_list = None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
pass | ||
return self.inorder_helper(self.root) | ||
|
||
def preorder_helper(self, current_node, node_list = None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(self, current_node, node_list = None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
return node_list | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
# Time Complexity: O(nlog(n)) | ||
# Space Complexity: O(m) where m is the number of end nodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ 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.