A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
sahar90
 2 years ago
i have a bst and i want to count the number of element of that in java language. and it is so important to run fast; do u think is it ok in all aspect?
int countTwoChildren(Node node) { return (node == null ? 0 : countTwoChildren(node.left) + countTwoChildren(node.right) + (node.left!=null && node.right!=null ? 1 : 0)); }
sahar90
 2 years ago
i have a bst and i want to count the number of element of that in java language. and it is so important to run fast; do u think is it ok in all aspect? int countTwoChildren(Node node) { return (node == null ? 0 : countTwoChildren(node.left) + countTwoChildren(node.right) + (node.left!=null && node.right!=null ? 1 : 0)); }

This Question is Closed

KonradZuse
 2 years ago
Best ResponseYou've already chosen the best response.0Yeah the code looks really ugly.... if anything with "speed" it's going to do the same calculations just return funky.... This doesn't work does it? do all your calculations, then return calculation.

sahar90
 2 years ago
Best ResponseYou've already chosen the best response.0tnx for ur help; do u think is it betther? public int nbNodes(Node root){ int count = 1 // our actual node if(root.left != null){ count += nbNodes(root.left); } if(root.right != null){ count += nbNodes(root.right); } return count; }

KonradZuse
 2 years ago
Best ResponseYou've already chosen the best response.0kind of hard to read, but yeah returning the variable count is the better choice.

sahar90
 2 years ago
Best ResponseYou've already chosen the best response.0excuse me; i'll be type it better to be useful for others; tnx for ur help again public int nbNodes(Node root){ int count = 1 if(root.left != null){ count += nbNodes(root.left); } if(root.right != null){ count += nbNodes(root.right); } return count; }
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.