I am working on my assignment of java in which I have given a question from java data structures:
The question is: Find the depth of the tree if total number of nodes are 20.
How can I find this ? Can anyone please help me ??
I am working on my assignment of java in which I have given a question from java data structures:
The question is: Find the depth of the tree if total number of nodes are 20.
How can I find this ? Can anyone please help me ??
Inputs that are needed before finding the depth of the tree:
In first case, you can traverse till the leaf using DFS and find the depth of tree(i.e.the length of the longest path from root to a leaf).
In second case, it is just a math work.
Without knowing any elaborating information on the class or lessons related to this question. Traversing the tree and counting the depth would be the basic answer. Here is a related topic.
Yes agree, it's very common topic to be asked.
I guess, your case your tree type may be binary tree, binary tree has fixed structure of nodes. As you see below 1,2,4,8,16.
*
* *
* * * *
In your case your tree depth will be 5. I hope you can write one of many logic. One simple logic is to find the binary representation of input number. For 20 it is 10100. Length of binary representation is length of binary tree.