# Bottom View of a Binary Tree using Recursion

Given a binary tree, the task is to find the bottom view of a binary tree using recursion.

Examples:

```Input:
1
\
2
\
4
/  \
3    5
Output: 1 3 4 5

Input:
20
/    \
8       22
/   \    /   \
5      10 21     25
/ \
9    14

Output: 5 9 21 14 25
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

We can do so by using recursion and 2 arrays each with size 2n+1(for worst case), where n = number of elements in the given tree. Here, we take a Variable x which determines its Horizontal Distance. Let x is the horizontal distance of a Node. Now, the left child will have a horizontal distance of x-1(x minus 1)and the right child will have horizontal distance x+1(x plus 1). Take another Variable ‘p’ as a priority which will decide which level this element belongs to.

```    1 (x=0, p=0)
\
2 (x=1, p=1)
\
4 (x=2, p=2)
/  \
(x=1, p=3) 3     5 (x=3, p=3)
```

While Traversing the Tree In fashion Right-> Node-> Left, assign x and p to each Node and simultaneously insert the data of node in the first array if the array is empty at position (mid+x). If the array is not empty and a Node with higher Priority( p ) comes to update the array with the data of this Node as position(mid+x). The second array will be maintaining the priority( p ) of each inserted node in the first array check code for better understanding.

Below is the implementation of above approach:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `struct` `Node { ` `    ``int` `data; ` `    ``// left and right references ` `    ``Node *left, *right; ` `    ``// Constructor of tree Node ` `    ``Node(``int` `key) ` `    ``{ ` `        ``data = key; ` `        ``left = right = NULL; ` `    ``} ` `}; ` ` `  `int` `l = 0, r = 0; ` `int` `N; ` ` `  `// Function to generate ` `// bottom view of ` `// binary tree ` `void` `Bottom(Node* root, ``int` `arr[], ``int` `arr2[], ``int` `x, ``int` `p, ``int` `mid) ` `{ ` `    ``// Base case ` `    ``if` `(root == NULL) { ` `        ``return``; ` `    ``} ` ` `  `    ``if` `(x < l) { ` `        ``// To store leftmost ` `        ``// value of x in l ` `        ``l = x; ` `    ``} ` ` `  `    ``// To store rightmost ` `    ``// value of x in r ` `    ``if` `(x > r) { ` `        ``r = x; ` `    ``} ` ` `  `    ``// To check if arr ` `    ``// is empty at mid+x ` `    ``if` `(arr[mid + x] == INT_MIN) { ` `        ``// Insert data of Node ` `        ``// at arr[mid+x] ` `        ``arr[mid + x] = root->data; ` `        ``// Insert priority of ` `        ``// that Node at arr2[mid+x] ` `        ``arr2[mid + x] = p; ` `    ``} ` ` `  `    ``// If not empty and priotiy ` `    ``// of previously inserted ` `    ``// Node is less than current*/ ` `    ``else` `if` `(arr2[mid + x] < p) { ` `        ``// Insert current ` `        ``// Node data at arr[mid+x] ` `        ``arr[mid + x] = root->data; ` ` `  `        ``// Insert priotiy of ` `        ``// that Node at arr2[mid +x] ` `        ``arr2[mid + x] = p; ` `    ``} ` ` `  `    ``// Go right first ` `    ``// then left ` `    ``Bottom(root->right, arr, arr2, x + 1, p + 1, mid); ` `    ``Bottom(root->left, arr, arr2, x - 1, p + 1, mid); ` `} ` ` `  `// Utility function ` `// to generate bottom ` `// view of a biany tree ` `void` `bottomView(``struct` `Node* root) ` `{ ` `    ``int` `arr[2 * N + 1]; ` `    ``int` `arr2[2 * N + 1]; ` ` `  `    ``for` `(``int` `i = 0; i < 2 * N + 1; i++) { ` `        ``arr[i] = INT_MIN; ` `        ``arr2[i] = INT_MIN; ` `    ``} ` ` `  `    ``int` `mid = N, x = 0, p = 0; ` ` `  `    ``Bottom(root, arr, arr2, x, p, mid); ` ` `  `    ``for` `(``int` `i = mid + l; i <= mid + r; i++) { ` `        ``cout << arr[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``N = 5; ` `    ``Node* root = ``new` `Node(1); ` `    ``root->right = ``new` `Node(2); ` `    ``root->right->right = ``new` `Node(4); ` `    ``root->right->right->left = ``new` `Node(3); ` `    ``root->right->right->right = ``new` `Node(5); ` ` `  `    ``bottomView(root); ` ` `  `    ``return` `0; ` `} `

## Python3

 `class` `Node:  ` `    ``def` `__init__(``self``, data):  ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` ` `  `l ``=` `0` `r ``=` `0` `INT_MIN ``=` `-``(``2``*``*``32``) ` ` `  `# Function to generate  ` `# bottom view of  ` `# binary tree  ` `def` `Bottom(root, arr, arr2, x, p, mid): ` `    ``global` `INT_MIN, l, r ` `     `  `    ``# Base case  ` `    ``if` `(root ``=``=` `None``): ` `        ``return` `     `  `    ``if` `(x < l): ` `         `  `        ``# To store leftmost  ` `        ``# value of x in l  ` `        ``l ``=` `x  ` `     `  `    ``# To store rightmost  ` `    ``# value of x in r  ` `    ``if` `(x > r): ` `        ``r ``=` `x  ` `         `  `    ``# To check if arr  ` `    ``# is empty at mid+x  ` `    ``if` `(arr[mid ``+` `x] ``=``=` `INT_MIN): ` ` `  `        ``# Insert data of Node  ` `        ``# at arr[mid+x]  ` `        ``arr[mid ``+` `x] ``=` `root.data  ` ` `  `        ``# Insert priority of  ` `        ``# that Node at arr2[mid+x]  ` `        ``arr2[mid ``+` `x] ``=` `p  ` `         `  `    ``# If not empty and priotiy  ` `    ``# of previously inserted  ` `    ``# Node is less than current*/  ` `    ``elif` `(arr2[mid ``+` `x] < p): ` ` `  `        ``# Insert current  ` `        ``# Node data at arr[mid+x]  ` `        ``arr[mid ``+` `x] ``=` `root.data  ` `         `  `        ``# Insert priotiy of  ` `        ``# that Node at arr2[mid +x]  ` `        ``arr2[mid ``+` `x] ``=` `p  ` `     `  `    ``# Go right first  ` `    ``# then left  ` `    ``Bottom(root.right, arr, arr2, x ``+` `1``, p ``+` `1``, mid)  ` `    ``Bottom(root.left, arr, arr2, x ``-` `1``, p ``+` `1``, mid)  ` ` `  `# Utility function  ` `# to generate bottom  ` `# view of a biany tree  ` `def` `bottomView(root): ` `    ``global` `INT_MIN ` `    ``arr ``=` `[``0``]``*``(``2` `*` `N ``+` `1``)  ` `    ``arr2 ``=` `[``0``]``*``(``2` `*` `N ``+` `1``) ` `     `  `    ``for` `i ``in` `range``(``2` `*` `N ``+` `1``): ` `        ``arr[i] ``=` `INT_MIN  ` `        ``arr2[i] ``=` `INT_MIN  ` `    ``mid ``=` `N ` `    ``x ``=` `0` `    ``p ``=` `0` `    ``Bottom(root, arr, arr2, x, p, mid)  ` `     `  `    ``for` `i ``in` `range``(mid ``+` `l,mid ``+` `r ``+` `1``): ` `        ``print``(arr[i], end ``=` `" "``) ` `         `  `# Driver code  ` `N ``=` `5` `root ``=` `Node(``1``)  ` `root.right ``=` `Node(``2``)  ` `root.right.right ``=` `Node(``4``)  ` `root.right.right.left ``=` `Node(``3``)  ` `root.right.right.right ``=` `Node(``5``)  ` ` `  `bottomView(root)  ` `     `  `# This code is contributed by SHUBHAMSINGH10 `

Output:

```1 3 4 5
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

7

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.