Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!