Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Paul Clapham
• Bear Bibeault
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
• Carey Brown
Bartenders:
• salvin francis
• Scott Selikoff
• fred rosenberger

# binary tree largest set of keys

Greenhorn
Posts: 12
T is a binary tree with "n" vertices. "maxSum" is the maximum amount of The keys in the path from the root to the leaf. Write a recursive static function that takes a binary tree and returns the largest set of keys. I dont know how to continue this code :
class Node{
String data;
Node left, right;
boolean val;
Node(String data){
this.data = data;
this.left = null;
this.right = null;
val = false; //help field
}
public String toString(){return ""+data;}
public String getData() {return data;}
public Node getLeft() {return left;}
public Node getRight() {return right;}
}// Node
public class BinaryTree {
private Node root;
public BinaryTree() {this.root = null;}
public static Integer maxSum(BinaryTree tree) {. . .}

Saloon Keeper
Posts: 7386
66
Welcome to the Ranch.

Your requirements state create a method that finds the max number of keys from THE root to THE leaf. "Root" makes sense, there's only one. Which "leaf" are we talking about? It doesn't mention anything about the method taking a "leaf" parameter. I'm going to guess that they mean the maximum number of  nodes from the root to ANY of the leaves.

Greenhorn
Posts: 12
i think also they mean the maximum number of  nodes from the root to ANY of the leaves.

Carey Brown
Saloon Keeper
Posts: 7386
66
Do you know how to write recursive methods?

The basic jist is to call the method on the left node and then the right node providing that they are not null. When you reach a leaf return a count of one, else, return the sum of the two method calls plus one.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?