Logo F2FInterview

C++ - Data Structures Interview Questions

Q   |   QA

The four different traversals of T are In order, Post order, Preorder and Level-by-level traversal.

Each operator node has exactly two branches
Each operand node has no branches, such trees are called expression trees.

A + B * C - D * E
What is the logic behind in order traversal? (This is for interviewee)
Strategy: Left-Root-Right.
In this traversal, if tree is not empty, we first traverse (in order) the left sub tree; then visit the root node of tree, and then traverse (in order) the right sub tree.

A B C * + D E * -
Logic behind traversal: First traverse left(T) (in post order); then traverse Right(T) (in post order); and finally visit root.

Strategy: Left-Right-Root strategy, i.e.
Traverse the left sub tree In Post order
Traverse the right sub tree in Post order.P Visit the root.

- +A*BC*DE
Logic: Visit root first; then recursively perform preorder traversal of Left(T); followed by pre order. traversal of Right(T)

Strategy: Root-Left-Right
traversal, i.e.
Visit the root
Traverse the left sub tree preorder.
Traverse the right sub tree preorder.

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: