logo CodeStepByStep logo

doublePositives

Language/Type: Java binary trees

Write a method named doublePositives that doubles all data values greater than 0 in a binary tree of integers. Your method accepts as its parameter a reference to a TreeNode representing the root of the tree and returns a new tree where each positive node's data value has doubled. For example, consider the following tree:

(-9 (3 (0)) (15 (12 (6) (-3)) (24)))

If a variable named tree refers to the root of this tree, the call of doublePositives(tree) would return the following new tree:

(-9 (6 (0)) (30 (24 (12) (-3)) (48)))

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) { ... }
}
Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.