We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you open up the classes above the method we are writing, you will see that self is a BinarySearchTree object and not a Node object. If you traverse through self.root instead of root, you are actually assigning new values to the whole tree.
The line "root = self.root" essentially pulls the top node out of the tree so we can traverse through the nodes in the while loop. It would have been clearer to use "current = self.root" instead because after the first traversal we are not actually at root anymore.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Search Tree : Insertion
You are viewing a single comment's thread. Return to all comments →
If you open up the classes above the method we are writing, you will see that self is a BinarySearchTree object and not a Node object. If you traverse through self.root instead of root, you are actually assigning new values to the whole tree.
The line "root = self.root" essentially pulls the top node out of the tree so we can traverse through the nodes in the while loop. It would have been clearer to use "current = self.root" instead because after the first traversal we are not actually at root anymore.