Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In java: Write a method named printLeaves that outputs the leaves of a binary tree of integers from right to left. Your method accepts as
In java:
Write a method named printLeaves that outputs the leaves of a binary tree of integers from right to left. Your method accepts as its parameter a TreeNode that refers to the root of the tree. For example, if a variable tree refers to the root of the following tree:
2____ / \ 8 1 / / \ 0 7 6 / \ 4 9
Then the call of printLeaves(tree); should produce the following output:
leaves: 9 4 0
If the tree does not have any leaves (an empty tree), simply print:
no leaves
Assume that you are interacting with TreeNodes as defined below:
public class TreeNode {
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode() { ... }
public TreeNode(int data) { ... }
public TreeNode(int data, TreeNode left, TreeNode right) { ... }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started