# Tree Traversals

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

Question 1 |

Following function is supposed to calculate the maximum depth or height of a Binary tree -- the number of nodes along the longest path from the root node down to the farthest leaf node.

int maxDepth(struct node* node) { if (node==NULL) return 0; else { /* compute the depth of each subtree */ int lDepth = maxDepth(node->left); int rDepth = maxDepth(node->right); /* use the larger one */ if (lDepth > rDepth) return X; else return Y; } }What should be the values of X and Y so that the function works correctly?

X = lDepth, Y = rDepth | |

X = lDepth + 1, Y = rDepth + 1 | |

X = lDepth - 1, Y = rDepth -1 | |

None of the above |

**Tree Traversals**

**Discuss it**

Question 1 Explanation:

If a tree is not empty, height of tree is
MAX(Height of Left Subtree, Height of Right Subtree) + 1
See program to Find the Maximum Depth or Height of a Tree for more details.

Question 2 |

What is common in three different types of traversals (Inorder, Preorder and Postorder)?

Root is visited before right subtree | |

Left subtree is always visited before right subtree | |

Root is visited after left subtree | |

All of the above | |

None of the above |

**Tree Traversals**

**Discuss it**

Question 2 Explanation:

The order of inorder traversal is
LEFT ROOT RIGHT
The order of preorder traversal is
ROOT LEFT RIGHT
The order of postorder traversal is
LEFT RIGHT ROOT
In all three traversals, LEFT is traversed before RIGHT

Question 3 |

The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is:

d e b f g c a | |

e d b g f c a | |

e d b f g c a | |

d e f g b c a |

**Tree Traversals**

**Discuss it**

Question 3 Explanation:

Below is the given tree.

a / \ / \ b c / \ / \ / \ / \ d e f g

Question 4 |

What does the following function do for a given binary tree?

int fun(struct node *root) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 0; return 1 + fun(root->left) + fun(root->right); }

Counts leaf nodes | |

Counts internal nodes | |

Returns height where height is defined as number of edges on the path from root to deepest node | |

Return diameter where diameter is number of edges on the longest path between any two nodes. |

**Tree Traversals**

**Discuss it**

Question 4 Explanation:

The function counts internal nodes.
1) If root is NULL or a leaf node, it returns 0.
2) Otherwise returns, 1 plus count of internal nodes in left subtree, plus count of internal nodes in right subtree.
See the following complete program.
1

Question 5 |

Which of the following pairs of traversals is not sufficient to build a binary tree from the given traversals?

Preorder and Inorder | |

Preorder and Postorder | |

Inorder and Postorder | |

None of the Above |

**Tree Traversals**

**Discuss it**

Question 5 Explanation:

Question 6 |

Consider two binary operators ' ' and '' with the precedence of operator being lower than that of the operator. Operator is right associative while operator is left associative. Which one of the following represents the parse tree for expression (7 3 4 3 2)? (GATE CS 2011)

A | |

B | |

C | |

D |

**Tree Traversals**

**Discuss it**

Question 6 Explanation:

Let us consider the given expression ().
Since the precedence of is higher, the sub-expression () will be evaluated first. In this sub-expression, would be evaluated first because is right to left associative. So the expression is evaluated as . Also, note that among the two operators, first one is evaluated before the second one because the associativity of is left to right.

Question 7 |

Which traversal of tree resembles the breadth first search of the graph?

Preorder | |

Inorder | |

Postorder | |

Level order |

**Tree Traversals**

**Discuss it**

Question 7 Explanation:

Breadth first search visits all the neighbors first and then deepens into each neighbor one by one. The level order traversal of the tree also visits nodes on the current level and then goes to the next level.

Question 8 |

Which of the following tree traversal uses a queue data structure?

Preorder | |

Inorder | |

Postorder | |

Level order |

**Tree Traversals**

**Discuss it**

Question 8 Explanation:

Level order traversal uses a queue data structure to visit the nodes level by level.

Question 9 |

Which of the following cannot generate the full binary tree?

Inorder and Preorder | |

Inorder and Postorder | |

Preorder and Postorder | |

None of the above |

**Tree Traversals**

**Discuss it**

Question 9 Explanation:

To generate a binary tree, two traversals are necessary and one of them must be inorder. But, a full binary tree can be generated from preorder and postorder traversals. Read the algorithm

**here**. Read Can tree be constructed from given traversals?Question 10 |

Consider the following C program segment

struct CellNode { struct CelINode *leftchild; int element; struct CelINode *rightChild; } int Dosomething(struct CelINode *ptr) { int value = 0; if (ptr != NULL) { if (ptr->leftChild != NULL) value = 1 + DoSomething(ptr->leftChild); if (ptr->rightChild != NULL) value = max(value, 1 + DoSomething(ptr->rightChild)); } return (value); }The value returned by the function DoSomething when a pointer to the root of a non-empty tree is passed as argument is (GATE CS 2004)

The number of leaf nodes in the tree | |

The number of nodes in the tree | |

The number of internal nodes in the tree | |

The height of the tree |

**Tree Traversals**

**Discuss it**

Question 10 Explanation:

Explanation: DoSomething() returns max(height of left child + 1, height of left child + 1). So given that pointer to root of tree is passed to DoSomething(), it will return height of the tree. Note that this implementation follows the convention where height of a single node is 0.

There are 33 questions to complete.