Skip to content
Advertisement

Iterative Inorder Traversal B-Tree

My end goal is to do a findKthElement function and the only way I can think of is to perform iterative inorder traversal so that I can keep a counter, which obviously doesn’t work if its recursive. I have tried my best at an implementation similar to a BST but its not working, just printing the same thing infinately. Here is my attempt:

JavaScript

Advertisement

Answer

keep a counter, which obviously doesn’t work if its recursive

There is no problem keeping a counter in a recursive solution. You just need to make sure it’s a mutable reference. For example:

JavaScript

If you’re familiar with streams the internal for loop could be:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement