Is it possible to get the height of a binary tree without using recursion or a stack/queue?

Viewed 656

I am writing a program in C that involves many calls to a function that returns the height of a binary tree. Initially I used recursion to do it, but that has soon come back to bite me because I am getting stack overflow errors (not due to infinite recursion). To fix this, I am attempting to modify the function to not use recursion and use iteration instead. Yes, it is possible to do this with a stack/queue, but I would much prefer to not have to.

I found a website that gives code that traverses the tree without recursion or stacks. Here is the link: http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

I have attempted to modify it so that instead of printing out each node, it measures the max depth, but I am not sure how I would do that. How would I do this.

1 Answers
Related