Question: Q: Re-implement the int depth() method to use a software stack, rather than recursion. Hint: you may need two stacks, or a stack a pairs

Q:

Re-implement the int depth() method to use a software stack, rather than recursion. Hint: you may need two stacks, or a stack a pairs of appropriate things.

Code:

public int depth(TreeNode node)

{

if (node == null)

return 0;

else

{

int ldepth = depth(node.left);

int rdepth = depth(node.right);

if (ldepth > rdepth)

return(ldepth+1);

else

return(rdepth+1);

}

}

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!