recursion - Determinig all leafs nodes in a tree -
recursion - Determinig all leafs nodes in a tree -
can guys help me method? had homecoming before didnt work either
private string leafnodes(treenode root, string leafs){ if (root.isleaf()) { leafs += integer.tostring(root.getdata()); } else { if(root.getleft() != null) { leafs += leafnodes(root.getleft(), leafs); } if (root.getright() != null) { leafs += leafnodes(root.getright(), leafs); } homecoming leafs; } homecoming leafs; }
the problem pass leafs
children , add together result current string same leaf can appear several times in returned string. can prepare way:
private string leafnodes(treenode root){ string leaves = ""; if (root.isleaf()) { leaves = integer.tostring(root.getdata()); } else { if(root.getleft() != null) { leaves += leafnodes(root.getleft()); } if (root.getright() != null) { leaves += leafnodes(root.getright()); } } homecoming leaves; }
recursion data-structures binary-tree
Comments
Post a Comment