# which traversal algorithm gives the sorted order in binary search tree?

### Mohammed

Guys, does anyone know the answer?

get which traversal algorithm gives the sorted order in binary search tree? from screen.

## How to print nodes of a binary search tree in sorted order?

Hello guys, recently one of my readers was asked about how do you print all nodes of a binary search tree in sorted order during a telephonic Java interview. Unfortunately, he didn’t know that…

## How to print nodes of a binary search tree in sorted order?

Grokking the Coding Interview: Patterns for Coding Questions

Hello guys, recently one of my readers was asked about **how do you print all nodes of a binary search tree in sorted orde**r during a telephonic Java interview. Unfortunately, he didn’t know that InOrder traversal can be used to print nodes in sorted order and he asked me after the interview.

In the past, I have shared the **best data structure courses** and data structure interview questions, and today, I will teach you about interesting and useful binary tree algorithms called InOrer traversal. We will also see an implementation using the Java programming language.

The **InOrder** traversal is one of the three popular ways to traverse a binary tree data structure, the other two being the preOrder and postOrder. During the in-order traversal algorithm, the left subtree is explored first, followed by root, and finally nodes on the right subtree.

You start traversal from the root then go to the left node, then again go to the left node until you reach a leaf node. At that point in time, you print the value of the node or mark it visited and move to the right subtree. Continuing the same algorithm until all nodes of the binary tree are visited.

The InOrder traversal is also known as the **left-node-right** or **left-root-right** traversal or **LNR** traversal algorithm.

Similar to the preOrder algorithm, it is also a depth-first algorithm because it explores the depth of a binary tree before exploring siblings. Since it is one of the fundamental binary tree algorithms it’s quite popular in programming interviews.

## 100+ Coding Interview Questions for Programmers

### Solve these frequently asked coding problems to do well on your next programming job interviews.

codeburst.io

These traversal algorithms are also the basis to learn more advanced binary tree algorithms, hence every programmer should learn, understand, and know how to implement in-order and other traversal algorithms.

The easiest way to implement the inOrder traversal algorithm in Java or any programming language is by using recursion. Since the binary tree is a recursive data structure, recursion is the natural choice for solving a tree-based problem. The inOrder() the method in the BinaryTree class implements the logic to traverse a binary tree using recursion.

From the Interview point of view, InOrder traversal is extremely important because it also prints nodes of a binary search tree in the **sorted order** but only if a given tree is a binary search tree.

If you remember, in BST, the value of nodes in the left subtree is lower than the root, and the values of nodes on the right subtree are higher than the root. The In order traversal literally means IN order, I mean, nodes are printed in the order or sorted order.

Btw, even though these three algorithms (pre-order, in-order, and post-order) are popular binary tree traversal algorithms but they are not the only ones. You also have other breadth-first ways to traverse a binary tree, like level order traversal (See **Data Structure and Algorithms: Deep Dive**).

## The recursive algorithm to implement InOrder traversal of a Binary tree

The recursive algorithm of inorder traversal is very simple. You just need to call the inOrder() method of BinaryTree class in the order you want to visit the tree. What is most important is to include the base case, which is key to any recursive algorithm.

For example, in this problem, the base case is you reach the leaf node and there is no more node to explore, at that point of time recursion starts to wind down. Here are the exact steps to traverse the binary tree using InOrder traversal:

visit left node

print value of the root

visit the right node and here is the sample code to implement this algorithm using recursion in Java:

private void inOrder(TreeNode node) {

if (node == null) { return; } inOrder(node.left);

System.out.printf("%s ", node.data);

inOrder(node.right);

}

Similar to the preOrder() method in the last example, there is another inOrder() method which exposes inorder traversal to the public and calls this private method which actually performs the InOrder traversal.

This is the standard way to write a recursive method which takes input, it makes it easier for a client to call the method.

public void inOrder() {

inOrder(root); }

You can see that we start with root and then recursive call the inOrder() method with node.left, which means we are going down on left subtree until we hit node == null, which means the last node was a leaf node.

At this point in time, the inOrder() method will return and execute the next line, which prints the node.data. After that it's again a recursive inOrder() call with node.right, which will initiate the same process again.

You can also check out **Data Structure and Algorithms Part 1 and 2** courses on Pluralsight to learn more about algorithms and how to design your own algorithms.

## Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order

Last Updated : 24 Jan, 2023

Read Discuss Courses Practice Video

Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree.

**Input:**

A Binary Search Tree

**Output:**

Inorder Traversal: 10 20 30 100 150 200 300

Preorder Traversal: 100 20 10 30 200 150 300

Postorder Traversal: 10 30 20 150 300 200 100

**Input:**

Binary Search Tree

**Output:**

Inorder Traversal: 8 12 20 22 25 30 40

Preorder Traversal: 22 12 8 20 30 25 40

Postorder Traversal: 8 20 12 25 40 30 22

Recommended Practice

Please try your approach on IDE first, before moving on to the solution.

Try It!

**Inorder Traversal****:**

Below is the idea to solve the problem:

At first traverse **left subtree **then visit the **root **and then traverse the **right subtree**.

Follow the below steps to implement the idea:

Traverse left subtree

Visit the root and print the data.

Traverse the right subtree

The **inorder traversal **of the BST gives the values of the nodes in sorted order. To get the decreasing order visit the right, root, and left subtree.

Below is the implementation of the inorder traversal.

## C++

// C++ code to implement the approach

#includeusing namespace std;

// Class describing a node of tree

class Node { public: int data; Node* left; Node* right; Node(int v) { this->data = v;

this->left = this->right = NULL;

} };

// Inorder Traversal

void printInorder(Node* node)

{ if (node == NULL) return;

// Traverse left subtree

printInorder(node->left);

// Visit node

cout << node->data << " ";

// Traverse right subtree

printInorder(node->right);

} // Driver code int main() { // Build the tree

Node* root = new Node(100);

root->left = new Node(20);

root->right = new Node(200);

root->left->left = new Node(10);

root->left->right = new Node(30);

root->right->left = new Node(150);

root->right->right = new Node(300);

// Function call

cout << "Inorder Traversal: ";

printInorder(root); return 0; }

## Java

## Python3

## C#

Java Python3 C# Javascript

**Output**

Inorder Traversal: 10 20 30 100 150 200 300

**Time complexity:**O(N), Where N is the number of nodes.

**Auxiliary Space:**O(h), Where h is the height of tree

## Preorder Traversal:

Below is the idea to solve the problem:

At first visit the **root **then traverse **left subtree **and then traverse the **right subtree**.

Follow the below steps to implement the idea:

Visit the root and print the data.

Traverse left subtree

Traverse the right subtree

Below is the implementation of the preorder traversal.

## C++

// C++ code to implement the approach

#includeusing namespace std;

// Class describing a node of tree

class Node { public: int data; Node* left; Node* right; Node(int v) { this->data = v;

this->left = this->right = NULL;

} };

// Preorder Traversal

void printPreOrder(Node* node)

{ if (node == NULL) return; // Visit Node

cout << node->data << " ";

// Traverse left subtree

printPreOrder(node->left);

// Traverse right subtree

printPreOrder(node->right);

} // Driver code int main() { // Build the tree

Node* root = new Node(100);

root->left = new Node(20);

root->right = new Node(200);

root->left->left = new Node(10);

root->left->right = new Node(30);

root->right->left = new Node(150);

root->right->right = new Node(300);

// Function call

cout << "Preorder Traversal: ";

printPreOrder(root);

return 0; }

## Java

## Python3

## C#

Java Python3 C# Javascript

**Output**

Preorder Traversal: 100 20 10 30 200 150 300

**Time complexity:**O(N), Where N is the number of nodes.

**Auxiliary Space:**O(H), Where H is the height of the tree

## Postorder Traversal:

Below is the idea to solve the problem:

At first traverse **left subtree **then traverse the **right subtree **and then visit the **root**.

Follow the below steps to implement the idea:

Traverse left subtree

Traverse the right subtree

Visit the root and print the data.

## How to print the nodes of a binary tree in sorted order

Contributor: Javin Paul

## How to print the nodes of a binary tree in sorted order

Javin Paul

Tired of LeetCode? 😩

Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Practice your skills in a hands-on, setup-free coding environment. 💪

Hello guys, in this shot you will learn how to print all the nodes of a binary tree in sorted order. The **in-order** traversal is one of the three popular ways to traverse a binary tree data structure, the other two being the pre-order and post-order. During the in-order traversal algorithm, the left subtree is explored first, followed by the root, and finally the nodes in the right subtree.

## The basic steps

You start traversing from the root, then go to the left node, then you again go to the left node until you reach a **leaf node**. At that point in time, you print the value of the node or mark it as visited and move to the right subtree. Continue the same algorithm until all nodes of the binary tree are visited. The in-order traversal is also known as the **left-node-right** or **left-root-right** traversal or the **LNR** traversal algorithm.

Similar to the pre-order algorithm, it is also a depth-first algorithm because it explores the depth of a binary tree before exploring siblings. Since it is one of the fundamental binary tree algorithms, it’s quite popular in programming interviews.

These traversal algorithms are also the basis for learning more advanced binary tree algorithms; hence, every programmer should learn, understand, and know how to implement them.

The easiest way to implement the in-order traversal algorithm in Java or any programming language is by using recursion. Since the binary tree is a recursive data structure, recursion is the natural choice for solving a tree-based problem. The inOrder() method in the BinaryTree class below implements the logic to traverse a binary tree using recursion.

From an interview point of view, in-order traversal is extremely important because it also prints the nodes of a binary search tree in sorted order, but only if a given tree is a binary search tree. If you remember, in BST, the values of nodes in the left subtree are lower than the root, and the values of nodes in the right subtree are higher than the root. The in-order traversal literally means IN order. I mean, the nodes are printed in sorted order.

Btw, even though these three algorithms (pre-order, in-order, and post-order) are popular binary tree traversal algorithms, they are not the only ones. You also have other breadth-first ways to traverse a binary tree, like level-order traversal.

## The recursive algorithm to implement in-order traversal

The recursive algorithm of an in-order traversal is very simple. You just need to call the inOrder() method of BinaryTree class in the order you want to visit the tree. It is extremely important that you include the base case, which is the key to any recursive algorithm.

For example, in this problem, the base case occurs when you reach the leaf node and there are no further nodes to explore. At that point in time, recursion starts to wind down. Here are the exact steps to traverse the binary tree using in-order traversal:

Visit left node

Print value of the root

Visit right node

Here is the basic code to implement this algorithm using recursion in Java:

private void inOrder(TreeNode node) {

if (node == null) { return; } inOrder(node.left);

System.out.printf("%s ", node.data);

inOrder(node.right);

}

There is another inOrder() method, which exposes the private method that actually performs the traversal.

This is the standard way to write a recursive method that takes an input. This makes it easier for a client to call the method.

public void inOrder() {

inOrder(root); }

In the code below, you can see that we start with the root and then recursively call the inOrder() method with node.left, which means we are going down the left subtree until we hit node == null (our base case).

At this point in time, the inOrder() method will return and execute the next line, which prints node.data. After that, it recursively calls inOrder() with node.right, which will initiate the same process again.

## Code

import java.util.Stack;

/*

* Java Program to traverse a binary search tree

* using inorder traversal without recursion

* and print all nodes in sorted order

* In InOrder traversal first left node is visited, followed by root

* and right node. * * input: * 40 * /\ * 20 50 * / \ \ * 10 30 60 * / / \ * 5 67 78 *

* output: 5 10 20 30 40 50 60 67 78

*/ public class main {

public static void main(String[] args) throws Exception { // construct the binary tree given in question

BinaryTree bt = BinaryTree.create();

// traversing binary tree using InOrder traversal using recursion

System.out.println("printing nodes of binary tree on InOrder using recursion"); bt.inOrder();

} } class BinaryTree {

static class TreeNode {

String data;

TreeNode left, right;

TreeNode(String value) {

this.data = value;

Guys, does anyone know the answer?