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

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

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!