• Post Reply Bookmark Topic Watch Topic
  • New Topic

java nodes at specific tree level  RSS feed

 
luca williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i'm trying to count how many nodes are at a specific level on my tree data structure using recursion, i have been trying but i just cant get it to work right.



if anyone could point me in the right direction i would appreciate it Thanks.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do this, I usually use recursion to traverse the tree to the desired depth, and once there just increment a instance variable and not try to return a count back up the calling chain.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically happens when you try to run it? Infinite loop?

Just from glancing at it, it looks like you're not decrementing lev before passing it into the functions to the right and the left. I believe if you fix that it will work (or at least have some new behavior that gets you closer), but I haven't tested it yet.
(edit) It also looks like res is being redeclared?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!