binary tree - a spesific node's height !

hello ! im looking for a method to deterine a certain node's hight in a binary tree (that is its distance from the root ) please enlighten me on how to do this if you can thank you
[209 byte] By [JungleRata] at [2007-10-2 12:17:15]
# 1
Traverse the BT and with every step increase a counter. When you get to the node you're looking for, return the counter.
prometheuzza at 2007-7-13 9:03:57 > top of Java-index,Other Topics,Algorithms...
# 2
thank you for replying (ans so quickly at that) can you please give me a code on how that shold be ? thanks again
JungleRata at 2007-7-13 9:03:57 > top of Java-index,Other Topics,Algorithms...
# 3
> thank you for replying (ans so quickly at that) You're welcome.> can you please give me a code on how that shold be ?No, I can't. Because I don't know how your TreeNode and Tree class look like.
prometheuzza at 2007-7-13 9:03:57 > top of Java-index,Other Topics,Algorithms...
# 4
> Traverse the BT and with every step increase a> counter. When you get to the node you're looking for,> return the counter.Or just add an attribute to you TreeNode class which holds the value for the degree/height of the node.
prometheuzza at 2007-7-13 9:03:57 > top of Java-index,Other Topics,Algorithms...
# 5

Algorithm: height (Tree, n)

if n is the root of Tree then

return 0;

else

return 1 + height(Tree, p) // p is the parent of n in Tree

Now, you may start writing your code.

buteForcea at 2007-7-13 9:03:57 > top of Java-index,Other Topics,Algorithms...