Select The Code Snippet Which Performs Preorder Traversal #521
Select the code snippet which performs pre-order traversal.
This multiple choice question (MCQ) is related to the book/course gs gs121 Data Structures and Algorithms. It can also be found in gs gs121 Binary Trees - Preorder Traversal - Quiz No.1.
Select the code snippet which performs pre-order traversal.
public void postorder(Tree root) { if (root == null) return; Stack<Tree> stk = new Stack<Tree>(); Tree node = root; while (!stk.isEmpty() || node != null) { while (node != null) { if (node.right != null) stk.add(node.left); stk.add(node); node = node.right; } node = stk.pop(); if (node.right != null && !stk.isEmpty() && node.right == stk.peek()) { Trees nodeRight = stk.pop(); stk.push(node); node = nodeRight; } else { System.out.print(node.data + " "); node = null; } } }
public void postorder(Tree root) { if (root == null) return; Stack<Tree> stk = new Stack<Tree>(); Tree node = root; while (!stk.isEmpty() || node != null) { while (node != null) { if (node.right != null) stk.add(node.right); stk.add(node); node = node.left; } node = stk.pop(); if (node.right != null && !stk.isEmpty() && node.right == stk.peek()) { Trees nodeRight = stk.pop(); stk.push(node); node = nodeRight; } else { System.out.print(node.data + " "); node = null; } } }
public void postorder(Tree root) { if (root == null) return; Stack<Tree> stk = new Stack<Tree>(); Tree node = root; while (!stk.isEmpty() || node != null) { while (node != null) { if (node.right != null) stk.add(node.right); stk.add(node); node = node.left; } node = stk.pop(); if (node.right != null) { Trees nodeRight = stk.pop(); stk.push(node); node = nodeRight; } else { System.out.print(node.data + " "); node = null; } } }
public void postorder(Tree root) { if (root == null) return; Stack<Tree> stk = new Stack<Tree>(); Tree node = root; while (!stk.isEmpty() || node != null) { while (node != null) { if (node.right != null) stk.add(node.left); stk.add(node); node = node.left; } node = stk.pop(); if (node.right != null) { Trees nodeRight = stk.pop(); stk.push(node); node = nodeLeft; } else { System.out.print(node.data + " "); node = null; } } }
Similar question(s) are as followings:
Online Quizzes of gs121 Data Structures and Algorithms
Binary Trees - Binary Search Tree - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Binary Search Tree - Quiz No.2
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Preorder Traversal - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes