Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Ionut Enescu
why not returning right after kth element was found?

Ionut Enescu

Jan 2, 2025

// If we've traversed exactly k nodes, this is our result.         if (count == k)             result = node.val; // why not returning right here? why do we need to go through the child nodes on the right subtree?         // Finally, traverse the right subtree.         traverse(node.right, k);

1

0

Comments
Comments
S
Sourav Rawat5 months ago

It actually does it at the very first line

if not node or self.count >= k:             return

On this page