-
need ur help plz
hi every body,,,i am new and this is first time i write,,,,
i have question i dont know how to write it
i should write a method called public nodeLevel(int k) which returns how many nodes in level k.
i have already wtrite the class binary tree ...
thank u
-
What you will be performing is a breadth first search and using a queue to track the nodes visited and the node levels. [This is a classic kind of BFS solved task.] You will count the number of nodes which are at level "k" - either visit all the nodes at level k-1 and tally their children, or actually visit the nodes at level "k" and tally your count as you visit them.
If you really wanted (needed) to, you could do this as a depth first search, to traverse down to each sucessor at level "k".
Last edited by nspils; 04-29-2006 at 09:19 AM.
-
i ve written this it is ok or not??
public nodelevel(int k){
if(root == null)
return 0;
else
return getnodelevel(root, 1);
}
private int getnodelevel(TreeNode root, int k){
if(root == null)
return 0;
else
return level +getnodelevel(root.left, k) + getnodelevel(root.right,k);
}
-
That's a nice recursive determining the depth of the tree. How are you going to capture the number of nodes at each level?
-
ok can u help me with this plz
-
What can you do to store (in your nodeLevel method) the values being returned by your getNodeLevel method? Once you have those values stored, what can you do to obtain a count of the number of nodes at level "k"?
-
i don't know how to complete it??
-
You would perform a breadth first search -
go to root, put it into a queue, add a value that it is level 0
whle the queue is not empty,
pop the root node off the queue, visit each of its children, pushing them onto the queue, noting they are level 1
pop each first child off the queue, increment your count of level 1 nodes, visit each of its chidren, pushing them onto the queue adding a note that they are level 2.
when all level 1 nodes are off the queue,
pop each grandchildren child off the queue, increment your count of level 2 nodes, visit each of their chidren, pushing them onto the queue adding a note that they are level 3.
when all level 2 nodes are off the queue,
etc.
Last edited by nspils; 05-06-2006 at 08:01 PM.
-
How are you building your binary tree?
-
what do you mean?
i have already write the binary tree
Similar Threads
-
By ootnitsuj in forum C++
Replies: 8
Last Post: 02-09-2006, 10:03 PM
-
By Apocalyp5e in forum Java
Replies: 11
Last Post: 12-22-2005, 08:03 PM
-
Replies: 4
Last Post: 08-10-2005, 10:30 AM
-
By nsbscool in forum Java
Replies: 1
Last Post: 05-02-2005, 08:01 AM
-
Replies: 0
Last Post: 05-14-2003, 03:25 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks