# Traversal technique for Binary Tree

Learn: In this article, we will learn about **Traversal technique for Binary tree with their algorithms and example**.

Submitted by Abhishek Kataria, on June 11, 2018

## Binary Tree

**A binary tree** is a finite collection of elements or it can be said it is made up of nodes. Where each node contains the left pointer, right pointer, and a data element. The root pointer points to the topmost node in the tree. When the binary tree is not empty, so it will have a root element and the remaining elements are partitioned into two binary trees which are called the left pointer and right pointer of a tree.

## Traversing in the Binary Tree

**Tree traversal** is the process of visiting each node in the tree exactly once. Visiting each node in a graph should be done in a systematic manner. If search result in a visit to all the vertices, it is called a traversal. There are basically three traversal techniques for a binary tree that are,

- Preorder traversal
- Inorder traversal
- Postorder traversal

### 1) Preorder traversal

To **traverse a binary tree in preorder**, following operations are carried out:

- Visit the root.
- Traverse the left sub tree of root.
- Traverse the right sub tree of root.

**Note:** Preorder traversal is also known as NLR traversal.

**Algorithm:**

Algorithm preorder(t) /*t is a binary tree. Each node of t has three fields: lchild, data, and rchild.*/ { If t! =0 then { Visit(t); Preorder(t->lchild); Preorder(t->rchild); } }

**Example:** Let us consider the given binary tree,

Therefore, the preorder traversal of the above tree will be: **7,1,0,3,2,5,4,6,9,8,10**

### 2) Inorder traversal

To traverse a binary tree in inorder traversal, following operations are carried out:

- Traverse the left most sub tree.
- Visit the root.
- Traverse the right most sub tree.

**Note:** Inorder traversal is also known as LNR traversal.

**Algorithm:**

Algorithm inorder(t) /*t is a binary tree. Each node of t has three fields: lchild, data, and rchild.*/ { If t! =0 then { Inorder(t->lchild); Visit(t); Inorder(t->rchild); } }

**Example:** Let us consider a given binary tree.

Therefore the inorder traversal of above tree will be: **0,1,2,3,4,5,6,7,8,9,10**

### 3) Postorder traversal

To traverse a binary tree in postorder traversal, following operations are carried out:

- Traverse the left sub tree of root.
- Traverse the right sub tree of root.
- Visit the root.

**Note:** Postorder traversal is also known as LRN traversal.

**Algorithm:**

Algorithm postorder(t) /*t is a binary tree .Each node of t has three fields: lchild, data, and rchild.*/ { If t! =0 then { Postorder(t->lchild); Postorder(t->rchild); Visit(t); } }

**Example:** Let us consider a given binary tree.

Therefore the postorder traversal of the above tree will be: **0,2,4,6,5,3,1,8,10,9,7**

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.