WalzoneInterview Prep
๐Ÿ“ž Interviewing soon? Practice with a realistic AI mock phone interview โ€” it calls you, then scores you. First 15 min FREE โ†’

Data Structures & Algorithms ยท Basic ยท question 19 of 100

What are the differences between in-order, pre-order, and post-order tree traversals? Provide a brief description of each traversal method.?

๐Ÿ“• Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers โ€” PDF + EPUB for $5

In-order, pre-order, and post-order are different traversal methods used to visit the nodes of a binary tree. Each traversal method visits the nodes in a different order, resulting in different outputs and different use cases.

In-order traversal: In an in-order traversal, the left subtree is visited first, followed by the root node, and then the right subtree. This traversal method is commonly used to visit the nodes of a binary search tree in sorted order, as it visits the nodes in ascending order of their values.

Here is an example of in-order traversal in Java:

public static void inOrderTraversal(Node root) {
    if (root != null) {
        inOrderTraversal(root.left);
        System.out.print(root.val + " ");
        inOrderTraversal(root.right);
    }
}

Pre-order traversal: In a pre-order traversal, the root node is visited first, followed by the left subtree, and then the right subtree. This traversal method is commonly used to create a copy of the binary tree, as it visits the nodes in the order in which they would be copied.

Here is an example of pre-order traversal in Java:

public static void preOrderTraversal(Node root) {
    if (root != null) {
        System.out.print(root.val + " ");
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
    }
}

Post-order traversal: In a post-order traversal, the left subtree is visited first, followed by the right subtree, and then the root node. This traversal method is commonly used to delete the binary tree, as it visits the nodes in the order in which they would be deleted.

Here is an example of post-order traversal in Java:

public static void postOrderTraversal(Node root) {
    if (root != null) {
        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.print(root.val + " ");
    }
}

Overall, the differences between in-order, pre-order, and post-order tree traversals lie in the order in which the nodes are visited. While in-order traversal visits the left subtree first, pre-order traversal visits the root node first, and post-order traversal visits the root node last. These traversal methods can be used for different purposes, such as visiting the nodes in sorted order, creating a copy of the binary tree, or deleting the binary tree.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Data Structures & Algorithms interview โ€” then scores it.
๐Ÿ“ž Practice Data Structures & Algorithms โ€” free 15 min
๐Ÿ“• Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers โ€” PDF + EPUB for $5

All 100 Data Structures & Algorithms questions ยท All topics