java - Gathering the value of the leaf nodes in a binary tree with a recursive method -


i have make class binary search tree school assignment, , 1 of methods must implement going return string of leaf node values, separated comma, ["leaf node 1",leaf node 2, leaf node 3]. going left right.

i have solve using recursive helping method, , i'm totally blank

this have far

public void leafnodes(node<t> n) {     if(n.left != null) leafnodes(n.left);     if(n.right != null) leafnodes(n.right);      if(n.left == null && n.right == null)     {         // in here?     } } 

tried editing after suggestion:

i tried adding this:  public arraylist<string> leafnodes(node<t> n) {     arraylist<string> list = new arraylist<>();      if(n.left != null) leafnoder(n.left);     if(n.right != null) leafnoder(n.right);      if(n.left == null && n.roight == null)     {         list.add(n.value.tostring());     }     return list; } 

the method takes use of helping method return empty string. or "[]".

   public string leafnodevalues() {     stringjoiner sj = new stringjoiner(", ", "[","]");      if(empty()) return sj.tostring();      arraylist<string> = leafnodes(rot);      for(int = 0; < a.size(); i++)     {         sj.add(a.get(i));     }     return sj.tostring(); } 

like this?

public arraylist<string> leafnodes(node<t> n) { arraylist<string> list = new arraylist<>();  if(n.left != null) list.addall(leafnoder(n.left)); if(n.right != null) list.addall(leafnoder(n.right));  if(n.left == null && n.roight == null) {     list.add(n.value.tostring()); } return list; 

}

answer

your leafnodes method return arraylist<string>. begin empty list, addall leafnodes(n.left), addall leafnodes(n.right), , if n leaf, add n list. finally, return list.

to desired result, can call leafnodes on root node, , use :

string.join(",", leafnodes(root)); 

explanation :

in binary tree, every node has 0 child (a leaf), 1 child or 2 children.

if leaf, value should written 1 element list, , returned parent node. that's

list.add(n.value.tostring());

return list

if node has children, value should not added list, children, grandchildren (or grandgrandchildren or ...) must leaves, needs pass list parent node. that's :

list.addall(leafnodes(n.left));

list.addall(leafnoder(n.left));

return list

example

here's example binary tree 6 nodes :

1  2   4   5  3   6 

and debugging information :

calling leafnodes(1)  calling leafnodes(2)   calling leafnodes(4)    4 leaf    returning [4] 4   calling leafnodes(5)    5 leaf    returning [5] 5   returning [4, 5] 2  calling leafnodes(3)   calling leafnodes(6)    6 leaf    returning [6] 6   returning [6] 3  returning [4, 5, 6] 1 

i hope makes clearer when calling , returning happens.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -