Geeks With Blogs
Fahd Siddiqui [Problem Solved] October 2010 Entries
Optimized way of finding a height of a tree (including non-binary trees)
The usual solution to find the height of a binary tree follows the recursive algorithm shown below: if node == null: return 0 else return max(height(node.left), height(node.right)) + 1 The usual algorithm won't help you if you are dealing with non binary trees. So, here's an alternate way of finding height of a tree. For purpose of demonstration, I am using a binary tree, but as I detail in conclusion, this applies to non-binary trees as well. First, add an additional attribute to your node called ......

Posted On Saturday, October 30, 2010 4:17 PM

Copyright © fahdsiddiqui | Powered by: