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
Get step-by-step solutions from verified subject matter experts
