Question
Given the above tree, its root stored in a TreeNode called root,: What is the output of the following code? Stack> stack = new Stack
Given the above tree, its root stored in a TreeNode called root,:
What is the output of the following code?
Stack> stack = new Stack (); stack .push(root ); do{
TreeNode node = stack.pop();
System . out . println (node . value );
if (node.left !=null) stack.push(node. left ); if (node.right != null) stack .push(node. right ); if (node.value == 5) break;
} while (!stack.isEmpty());
What is the output of the following code?
Queue> stack = new LinkedList (); s t a c k . add ( r o o t ) ; do{
TreeNode node = stack.remove(); System . out . println (node . value );
if (node.value==5) break; if (node.left !=null) stack.add(node. left ); if (node.right != null) stack .add(node. right );
} while (!stack.isEmpty());
What is the name of the searches performed in step 1 and 2?
50 39 25 36 15 10
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