## Prune nodes not on paths with given sum

Prune nodes not on paths with given sum is a very commonly asked question in Amazon interviews. It involves two concepts in one problem. First, how to find a path with a given sum and then second, how to prune nodes from binary tree. The problem statement is:

Given a binary tree, prune nodes which are not paths with a given sum.

For example, given the below binary tree and given sum as 43, red nodes will be pruned as they are not the paths with sum 43.

### Prune nodes in a binary tree: thoughts

To solve this problem, first, understand how to find paths with a given sum in a binary tree.  To prune all nodes which are not on these paths,  get all the nodes which are not part of any path and then delete those nodes one by one. It requires two traversals of the binary tree.
Is it possible to delete a node while calculating the path with a given sum? At what point we find that this is not the path with given sum? At the leaf node.
Once we know that this leaf node is not part of the path with given sum, we can safely delete it.  What happens to this leaf node? We directly cannot delete the parent node as there may be another subtree which leads to a path with the given sum. Hence for every node, the pruning is dependent on what comes up from its subtrees processing.

At the leaf node, we return to parent false if this leaf node cannot be part of the path and delete the leaf node. At parent node, we look for return values from both the subtrees. If both subtrees return false, it means this node is not part of the path with the given sum. If one of the subtrees returns true, it means the current node is part of a path with the given sum. It should not be deleted and should return true to its parent.

#### Prune nodes from a binary tree: implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define true 1
#define false 0

int prunePath(Node *node, int sum ){

if( !node ) return true;

int subSum =  sum - node->value;
/* To check if left tree or right sub tree
contributes to total sum  */

int leftVal = false, rightVal = false;

/*Check if node is leaf node */
int isLeaf = !( node->left || node->right );

/* If node is leaf node and it is part of path with sum
= given sum return true to parent node so tha parent node is
not deleted */
if(isLeaf && !subSum )
return true;

/* If node is leaf and it not part of path with sum
equals to given sum
Return false to parent node */
else if(isLeaf && subSum ){
free(node);
return false;
}
/* If node is not leaf and there is left child
Traverse to left subtree*/
leftVal = prunePath(node->left, subSum);

/* If node is not leaf and there is right child
Traverse to right subtree*/
rightVal = prunePath(node->right, subSum);

/* This is crux of algo.
1. If both left sub tree and right sub tree cannot lead to
path with given sum,Delete the node
2. If any one sub tree can lead to path with sum equal
to given sum, do not delete the node */
if(!(leftVal || rightVal) ){
free(node);
return false;
}
if(leftVal || rightVal ){
if(leftVal)
node->right = NULL;
if(rightVal)
node->left = NULL;
return true;
}
return true ;
}

void inoderTraversal(Node * root){
if(!root)
return;

inoderTraversal(root->left);
printf("%d ", root->value);
inoderTraversal(root->right);
}
Node *createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));
newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}
Node *addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
root = addNode(root,37);
root = addNode(root,45);

inoderTraversal(root);
prunePath(root, 65);

printf( "\n");
if( root ){
inoderTraversal(root);
}
return 0;
}

The complexity of this algorithm to prune all nodes which are not on the path with a given sum is O(n).

Please share if there is something wrong or missing. If you are preparing for interviews, please signup for free interview material.

## Print paths in a binary tree

We learned various kind of traversals of a binary tree like inorder, preorder and postorder. Paths in a binary tree problem require traversal of a binary tree too like every other problem on a binary tree. The problem statement is:

Given a binary tree, print all paths in that binary tree

What is a path in a binary tree? A path is a collection of nodes from the root to any leaf of the tree. By definition, a leaf node is a node which does not have left or right child. For example, one of the paths in the binary tree below is 10,7,9.

### Paths in a binary tree: thoughts

It is clear from the problem statement that we have to start with root and go all the way to leaf nodes. Question is do we need to start with root from each access each path in binary tree? Well, no. Paths have common nodes in them. Once we reach the end of a path (leaf node), we just move upwards one node at a time and explore other paths from the parent node. Once all paths are explored, we go one level again and explore all paths from there.

This is a typical postorder traversal of a binary tree, we finish the paths in the left subtree of a node before exploring paths on the right subtree We process the root before going into left or right subtree to check if it is the leaf node. We add the current node into the path till now. Once we have explored left and right subtree, the current node is removed from the path.

Let’s take an example and see how does it work. Below is the tree for each we have to print all the paths in it.

First of all our list of paths is empty. We have to create a current path, we start from the root node which is the node(10). Add node(10) to the current path. As node(10) is not a leaf node, we move towards the left subtree.

node(7) is added to the current path. Also, it is not a leaf node either, so we again go down the left subtree.

node(8) is added to the current path and this time, it is a leaf node. We put the entire path into the list of paths or print the entire path based on how we want the output.

At this point, we take outnode(8) from the current path and move up to node(7). As we have traversed the left subtree of,node(7) we will traverse right subtree of the node(7).

node(9) is added now to the current path. It is also a leaf node, so again, put the path in the list of paths. node(9) is moved out of the current path.

Now, left and right subtrees of node(7) have been traversed, we remove node(7) from the current path too.

At this point, we have only one node in the current path which is the node(10) We have already traversed the left subtree of it. So, we will start traversing the right subtree, next we will visit node(15) and add it to the current path.

node(15) is not a leaf node, so we move down the left subtree. node(18) is added to the current path. node(18) is a leaf node too. So, add the entire path to the list of paths. Remove node(18) from the current path.

We go next to the right subtree of the node(15), which is the node(19). It is added to the current path. node(19) is also a leaf node, so the path is added to the list of paths.

Now, the left and right subtrees of the node(15) are traversed, it is removed from the current path and so is the node(10).

#### Print paths in a binary tree: implementation

package com.company.BST;

import java.util.ArrayList;

/**
* Created by sangar on 21.10.18.
*/
public class PrintPathInBST {
public void printPath(BinarySearchTree tree){
ArrayList<TreeNode> path  = new ArrayList<>();
this.printPathRecursive(tree.getRoot(), path);
}

private void printPathRecursive(TreeNode root,
ArrayList<TreeNode> path){
if(root == null) return;

path.add(root);

//If node is leaf node
if(root.getLeft() == null && root.getRight() == null){
path.forEach(node -> System.out.print(" "
+ node.getValue()));
path.remove(path.size()-1);
System.out.println();
return;
}

/*Not a leaf node, add this node to
path and continue traverse */
printPathRecursive(root.getLeft(),path);
printPathRecursive(root.getRight(), path);

//Remove the root node from the path
path.remove(path.size()-1);
}
}

Test cases

package com.company.BST;

/**
* Created by sangar on 10.5.18.
*/
public class BinarySearchTreeTests {
public static void main (String[] args){
BinarySearchTree binarySearchTree = new BinarySearchTree();

binarySearchTree.insert(7);
binarySearchTree.insert(8);
binarySearchTree.insert(6);
binarySearchTree.insert(9);
binarySearchTree.insert(3);
binarySearchTree.insert(4);

binarySearchTree.printPath();
}
}

Tree node definition

package com.company.BST;

/**
* Created by sangar on 21.10.18.
*/
public class TreeNode<T> {
private T value;
private TreeNode left;
private TreeNode right;

public TreeNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}

public T getValue(){
return this.value;
}
public TreeNode getRight(){
return this.right;
}
public TreeNode getLeft(){
return this.left;
}

public void setValue(T value){
this.value = value;
}

public void setRight(TreeNode node){
this.right = node;
}

public void setLeft(TreeNode node){
this.left = node;
}
}

Complexity of above algorithm to print all paths in a binary tree is O(n).

Please share if there is something wrong or missing. If you are preparing for Amazon, Microsoft or Google interviews, please signup for interview material for free.

# Inorder predecessor in binary search tree

What is an inorder predecessor in binary tree? Inorder predecessor is the node which traversed before given node in inorder traversal of binary tree.  In binary search tree, it’s the previous big value before a node. For example, inorder predecessor of node(6) in below tree will 5 and for node(10) it’s 6.

If node is leftmost node in BST or least node, then there is no inorder predecessor for that node.

## Inorder predecessor : Thoughts

To find inorder predecessor , first thing to find is the node itself.  As we know in inorder traversal, root node is visited after left subtree.  A node can be predecessor for given node which is on right side of it.

Let’s come up with examples and see what algorithm works. First case, if given node is left most leaf node of tree, there is no inorder predecessor, in that case return NULL. For example, predecessor for node 1 is NULL.

What if node has left subtree? In that case, maximum value in left subtree will be predecessor of given node.  We can find maximum value in tree by going deep down right subtree, till right subtree is NULL, and then return the last node. For example, predecessor node(10) is 6.

What are the other cases? Another case is that node does not have left subtree but it is also not the leftmost leaf node? Then parent of given node will be inorder predecessor. While moving down the tree on right side, keep track of parent node as it may be solution. predecessor of node(12) will be 10 as that’s where we moved to right subtree last time.  Note that we change predecessor candidate only  while moving down right subtree.

### Algorithm to find inorder predecessor

1. Start with root, current = root, successor = NULL.
2. If node.value > current.value, then predecessor = current, current = current.right.
3. If node.value < current.value, current = current.left.
4. If node.value == current.value and node.left!= null, predecessor = maximum(current.left).
5. Return predecessor

### Inorder predevessor: Implementation

#include<stdio.h>
#include<stdlib.h>

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

/* This function return the maximum node in tree rooted at node root */
Node *findMaximum(Node *root){
if(!root)
return NULL;

while(root->right) root = root->right;
return root;
}
/* This function implements the logic described in algorithm to find inorder predecessor
of a given node */
Node *inorderPredecessor(Node *root, int K){

Node *predecessor 	= NULL;
Node *current  		= root;

if(!root)
return NULL;

while(current && current->value != K){
/* Else take left turn and no need to update predecessor pointer */
if(current->value >K){
current= current->left;
}
/* If node value is less than the node which are looking for, then go to right sub tree
Also when we move right, update the predecessor pointer to keep track of last right turn */
else{
predecessor = current;
current = current->right;
}
}
/*Once we reached at the node for which inorder predecessor is to be found,
check if it has left sub tree, if yes then find the maximum in that right sub tree and return that node
Else last right turn taken node is already stored in predecessor pointer and will be returned*/
if(current && current->left){
predecessor = findMaximum(current->left);
}
return predecessor;
}
Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;

}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
int n = 0;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
root = addNode(root,37);
root = addNode(root,45);

Node *predecessor = inorderPredecessor(root, 40);
printf("\n Inorder successor node is : %d ",predecessor ? predecessor->value: 0);

return 0;
}

Complexity of algorithm to find inorder predecessor will be O(logN) in almost balanced binary tree. If tree is skewed, then we have worst case complexity of O(N).

Please share if there is something wrong or missing. If you want to contribute to website and share your knowledge with thousands of learners across the world, please reach out to us at communications@algorithmsandme.com

# Inorder successor in binary search tree

What is an inorder successor in binary tree? Inorder successor is the node which traversed next to given node in inorder traversal of binary tree.  In binary search tree, it’s the next big value after the node. For example, inorder successor of node(6) in below tree will 10 and for node(12) it’s 14.

If node is the rightmost node or in BST, the greatest node, then there is no inorder successor for that node.

## Inorder successor : Thoughts

To find inorder successor, first thing to find is the node itself.  As we know in inorder traversal, root node is visited after left subtree.  A node can be successor for given node which is on left side of it.

Let’s come up with examples and see what algorithm works. First case, if the node is right most node of tree, there is no inorder successor, in that case return NULL. For example, successor for node 16 is NULL.

What if node has right subtree? In that case, minimum value in right subtree will be successor of given node.  We can find minimum value in tree by going deep down left subtree, till left subtree is NULL, and then return the last node. For example, successor node(5) is 6.

What are the other cases? Another case is that node does not have right subtree? Then parent of given node will be inorder successor. While moving down the tree on left side, keep track of parent node as it may be the solution. Successor of node(7) will be 10 as that’s where we moved to left subtree last time.  Note that we change successor candidate only  while moving down left subtree.

### Algorithm to find inorder successor

1. Start with root, current = root, successor = NULL.
2. If node.value < current.value, then successor = current, current = current.left.
3. If node.value > current.value, current = current.right.
4. If node.value == current.value and node.right != null, successor = minimum(current.right).
5. Return successor

### Inorder successor : Implementation

#include<stdio.h>
#include<stdlib.h>

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

//this function finds the minimum node in given tree rooted at node root
Node * findMinimum(Node *root){
if(!root)
return NULL;
// Minimum node is left most child. hence traverse down till left most node of tree.
while(root->left) root = root->left;
// return the left most node
return root;
}
/* This function implements the logic described in algorithm to find inorder successor
of a given node */
Node *inorderSuccessor(Node *root, Node *node){

Node *successor = NULL;
Node *current  = root;
if(!root)
return NULL;

while(current->value != node->value){
/* If node value is greater than the node which are looking for, then go to left sub tree
Also when we move left, update the successor pointer to keep track of lst left turn */

if(current->value > node->value){
successor = current;
current= current->left;
}
/* Else take right turn and no need to update successor pointer */
else
current = current->right;
}
/*Once we reached at the node for which inorder successor is to be found,
check if it has right sub tree, if yes then find the minimum in that right sub tree and return taht node
Else last left turn taken node is already stored in successor pointer and will be returned*/
if(current && current->right){
successor = findMinimum(current->right);
}

return successor;
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;

}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
int n = 0;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
Node *node = root;
root = addNode(root,37);
root = addNode(root,45);

Node *successor = inorderSuccessor(root, node);
printf("\n Inorder successor node is : %d ",successor ? successor->value: 0);

return 0;
}

Complexity of algorithm to find inorder successor will be O(logN) in almost balanced binary tree. If tree is skewed, then we have worst case complexity of O(N).

Please share if there is something wrong or missing. If you want to contribute to website and share your knowledge with thousands of learners across the world, please reach out to us at communication@algorithmsandme.com

# Iterative postorder traversal

In last two posts, iterative inorder and iterative preorder traversal, we learned how stack can be used to replace recursion and why recursive implementation can be dangerous in production environment. In this post, let’s discuss iterative postorder traversal of binary tree which is most complex of all traversals. What is post order traversal ? A traversal where  left and right subtrees are visited before root is processed. For example, post order traversal of below tree would be : [1,6,5,12,16,14,10]

## Iterative postorder traversal  : Thoughts

Let’s look at the recursive implementation of postorder.

private void postOrder(Node root){
if(root == null) return;

postOrder(root.left);
postOrder(root.right);
System.out.println(root.value);

}

As we are going into left subtree and then directly to right subtree, without visiting root node. Can you find the similarity of structure between preorder and postorder implementation?  Can we reverse the entire preorder traversal to get post order traversal? Reverse preorder will give us right child, left child and then root node, however order expected is left child, right child and root child.
Do you remember we pushed left and right node onto stack in order where right child went before left. How about reversing that?

There is one more problem with just reversing the preorder. In preorder, a node was processed as soon as popped from stack, like root node will  be the first node to be processed. However, in postorder, root node is processed last. So, we actually need the order of processing too be reversed. What better than using a stack to store the reverse order of root nodes to processed.
All in all, we will be using two stacks, one to store left and right child, second to store processing order of nodes.

1. Create two stacks s an out and push root node onto s
2. While stack s is not empty
1. op from stack s, current = s.pop
2. Put current onto stack out.
3. Put left and right child of current on to stack s
3. Pop everything from out stack and process it.

### Postorder traversal with two stacks : Implementation

package com.company.BST;

import java.util.Stack;

/**
* Created by sangar on 22.5.18.
*/
public class BinarySearchTreeTraversal {

private Node root;

public void BinarySearchTree(){
root = null;
}

public class Node {
private int value;
private Node left;
private Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}

public void insert(int value){
this.root =  insertNode(this.root, value);
}

private Node insertNode(Node root, int value){
if(root == null){
//if this node is root of tree
root = new Node(value);
}
else{
if(root.value > value){
//If root is greater than value, node should be added to left subtree
root.left = insertNode(root.left, value);
}
else{
//If root is less than value, node should be added to right subtree
root.right = insertNode(root.right, value);
}
}
return root;
}
private void postOrder(Node root){
if(root == null) return;

postOrder(root.left);
postOrder(root.right);
System.out.println(root.value);
}

public void postOrderTraversal(){
postOrderIterative(root);
}

private void postOrderIterative(Node root){
Stack<Node> out = new Stack<>();
Stack<Node> s = new Stack<>();

s.push(root);

while(!s.empty()){
Node current = s.pop();

out.push(current);
if(current.left != null) s.push(current.left);
if(current.right != null) s.push(current.right);
}

while(!out.empty()){
System.out.println(out.pop().value);
}
}
}

Complexity of iterative implementation is O(n) with additional space complexity of O(n).

Can we avoid using two stack, and do it with one stack? Problem with root in postorder traversal is that it is visited three times, moving down from parent, coming up from left child and coming up from right child. When should be the node processed? Well, when we are coming up from right child.

How can we keep track of how the current node was reached? If we keep previous pointer, there are three cases:

1. Previous node is parent of current node, we reached node from parent node, nothing is done.
2. Previous node is left child of current node, it means we have visited left child, but still not visited right child, move to right child of current node.
3. Previous node is right child of current node, it means  we have visited left and right child of current node,  process the current node.

Let’s formulate  postorder traversal algorithm then.

1. Push root node onto stack s, set prev = null.
2. Repeat below steps till stack is not empty (!s.empty())
3. current = s.pop(), pop from the stack.
4. If (prev == null || prev.left == current || prev.right == current) then
1. If current.left != null, push current.left onto stack.
2. If current.right != null, push current.right onto stack.
3. If current.left == current.right == null, process current.
5. If current.left == prev, i.e. moving up left child then
1. If current.right == null, process current.
2. If current.right != null, push it to stack.
6. If current.right == prev i.e moving up from right child
1. process current.
2. prev = current, current = s.pop.

### Iterative Postorder traversal : Implementation

package com.company.BST;

import java.util.Stack;

/**
* Created by sangar on 22.5.18.
*/
public class BinarySearchTreeTraversal {

private Node root;

public void BinarySearchTree(){
root = null;
}

public class Node {
private int value;
private Node left;
private Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}

public void insert(int value){
this.root =  insertNode(this.root, value);
}

private Node insertNode(Node root, int value){
if(root == null){
//if this node is root of tree
root = new Node(value);
}
else{
if(root.value > value){
//If root is greater than value, node should be added to left subtree
root.left = insertNode(root.left, value);
}
else{
//If root is less than value, node should be added to right subtree
root.right = insertNode(root.right, value);
}
}
return root;
}

private void inorder(Node root){
if(root == null) return;

if(root.left != null) inorder(root.left);
System.out.println(root.value);
if(root.right != null) inorder(root.right);
}

private void preOrder(Node root){
if(root == null) return;

System.out.println(root.value);
preOrder(root.left);
preOrder(root.right);
}

private void postOrder(Node root){
if(root == null) return;

postOrder(root.left);
postOrder(root.right);
System.out.println(root.value);

}
public void postOrderTraversal(){
//  postOrder(root);
postOrderIterative2(root);
//postOrderIterative(root);
}

private void postOrderIterative2(Node root){
Node prev = null;
Stack<Node> s = new Stack<>();

s.push(root);

while(!s.empty()){
Node current  = s.peek();
if(prev == null || ( prev.left == current || prev.right == current )){
if(current.left != null) s.push(current.left);
else if(current.right != null) s.push(current.right);
}
else if(prev == current.left){
if(current.right != null) s.push(current.right);
}else{
System.out.println(current.value);
s.pop();
}

prev = current;
}
}

}

Complexity of code is O(n) again, with additional space complexity of O(n).

Please share if there is something wrong or missing. If you want to contribute and share your learning with thousands of learners across the world, please reach out to us at communications@algorithmsandme.com

# Iterative preorder traversal

In last post Iterative inorder traversal , we learned how to do inorder traversal of binary tree without recursion or in iterative way. Today we will learn how to do iterative preorder traversal of binary tree. In preorder traversal, root node is processed before left and right subtrees. For example, preorder traversal of below tree would be [10,5,1,6,14,12,15],

We already know how to implement preorder traversal in recursive way, let’s understand how to implement it in non-recursive way.

## Iterative preorder traversal : Thoughts

If we look at recursive implementation, we see we process the root node as soon as we reach it and then start with left subtree before touching anything on right subtree.

Once left subtree is processed, control goes to first node in right subtree. To emulate this behavior in non-recursive way, it is best to use a stack. What and when push and pop will happen on the stack?
Start with pushing the root node to stack. Traversal continues till there at least one node onto stack.

Pop the root node from stack,process it and push it’s right and left child on to stack. Why right child before left child? Because we want to process left subtree before right subtree. As at every node, we push it’s children onto stack, entire left subtree of node will be processed before right child is popped from the stack. Algorithm is very simple and is as follows.

1. Start with root node and push on to stack s
2. While there stack is not empty
1. Pop from stack current  = s.pop() and process the node.
2. Push current.right onto to stack.
3. Push current.left onto to stack.

### Iterative preorder traversal : example

Let’s take and example and see how it works. Given below tree, do preorder traversal on it without recursion.

Let’s start from root node(10) and push it onto stack. current = node(10).

Here loop starts, which check if there is node onto stack. If yes, it pops that out. s.pop will return node(10), we will print it and push it’s right and left child onto stack. Preorder traversal till now : [10].

Since stack is not empty, pop from it.current= node(5). Print it and push it’s right and left child i.e node(6) and node(1) on stack.

Again, stack is not empty, pop from stack. current  = node(1). Print node. There is no right and left child for this node, so we will not push anything on the stack.

Stack is not empty yet, pop again. current= node(6). Print node. Similar to node(1), it also does not have right or left subtree, so nothing gets pushed onto stack.

However, stack is not empty yet. Pop. Current = node(14). Print node, and as there are left and right children, push them onto stack as right child before left child.

Stack is not empty, so pop from stack, current = node(12). Print it, as there are no children of node(12), push nothing to stack.

Pop again from stack as it not empty. current = node(15). Print it. No children, so no need to push anything.

At this point, stack becomes empty and we have traversed all node of tree also.

### Iterative preorder traversal : Implementation

#include <stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define STACK_SIZE 10

typedef struct stack{
int top;
Node *items[STACK_SIZE];
}stack;

void push(stack *ms, Node *item){
if(ms->top < STACK_SIZE-1){
ms->items[++(ms->top)] = item;
}
else {
printf("Stack is full\n");
}
}

Node * pop (stack *ms){
if(ms->top > -1 ){
return ms->items[(ms->top)--];
}
else{
printf("Stack is empty\n");
}
}
Node * peek(stack ms){
if(ms.top < 0){
printf("Stack empty\n");
return 0;
}
return ms.items[ms.top];
}
int isEmpty(stack ms){
if(ms.top < 0) return 1;
else return 0;
}
void preorderTraversalWithoutRecursion(Node *root){
stack ms;
ms.top = -1;

if(root == NULL) return ;

Node *currentNode = NULL;
/* Step 1 : Start with root */
push(&ms,root);

while(!isEmpty(ms)){
/* Step 5 : Pop the node */
currentNode = pop(&ms);
/* Step 2 : Print the node */
printf("%d  ", currentNode->value);
/* Step 3: Push right child first */
if(currentNode->right){
push(&ms, currentNode->right);
}
/* Step 4: Push left child */
if(currentNode->left){
push(&ms, currentNode->left);
}
}
}

void preorder (Node *root){
if ( !root ) return;

printf("%d ", root->value );
preorder(root->left);
preorder(root->right);
}

Node * createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
root = addNode(root,37);
root = addNode(root,45);

preorder(root);
printf("\n");

preorderTraversalWithoutRecursion(root);
return 0;
}

Complexity of iterative implementation of binary tree is O(n) as we will be visiting each node at least once. Also, there is added space complexity of stack which is O(n).

Please share if there is something wrong or missing. If you are willing to contribute and share your knowledge with thousands of learners across the world, please reach out to us at communications@algorithmsandme.com

# Iterative Inorder traversal

One of the most common things we do on binary tree is traversal. In Binary search tree traversals we discussed different types of traversals like inorder, preorder and postorder traversals. We implemented those traversals in recursive way. In this post, let’s focus on iterative implementation of inorder traversal or iterative inorder traversal without recursion.

Before solution, what is inorder traversal of binary tree? In inorder traversal, visit left subtree, then root and at last right subtree. For example, for given tree, inorder traversal would be: [1,5,6,10,12,14,15]

## Iterative inorder traversal without stack : Thoughts

As we go into discussion, one quick question : why recursive inorder implementation is not that great? We know that recursion uses implicitly stack to store return address and passed parameters.  As recursion goes deep, there will be more return addresses and parameters stored on stack, eventually filling up all the space system has for stack. This problem is known as stack overflow.
When binary tree is skewed, that is when every node has only one child, recursive implementation may lead to stack overflow, depending on the size of tree. In production systems, we usually do not know upfront size of data structures, it is advised to avoid recursive implementations.

What are we essentially doing in recursive implementation?  We check if node is null, then return. If not, we move down the left subtree. When there is nothing on left subtree, we move up to parent, and then go to right subtree.

All these steps are easy to translate in iterative way. One thing needs to be thought of is : how to go to parent node? In inorder traversal, the last node visited before current node is the parent node.
If we keep these nodes on some structure, where we can refer them back, things will be easy.  As we refer the most recent node added to structure first (when finding parent of node, we have to just look at the last visited node), stack is great candidate for it which has last in first out property.

### Iterative inorder traversal : algorithm

1. Start from the root, call it current .
2. If current is not NULL, push current on to stack.
3. Move to left child of current and go to step 2.
4. If current  == NULL and !stack.empty(),  current = s.pop.
5. Process current and set current = current.right, go to step 2.

Let’s take an example and see how this algorithm works.

We start with node(10), current = node(10). Current node is not null, put it on stack.

As there is left child of node(10), move current = current.left, so current = node(5), which is not null, put node on to stack.

Again, move down to left child of node(5), current = current.left = node(1). Put the node on to stack.

Again move down to left child, which in this case it is null. What to do now? As stack is not empty, pop last node added to it. current = node(1). Process node(1). Traversal  = [1]

Move to right child of node(1), which is null, in that case pop from the stack and process the node, current  = node(5). Traversal = [1,5]

Move to the right child of node(5) i.e. node(6). Push on to the stack.

Move down to left subtree, which is null, so pop from stack. current = node(6), process it. Traversal = [1,5,6]

Move to right child of node(6), which is null, so pop from stack current = node(10). Process the node. Traversal = [1,5, 6,10]

Get right child of node(10), which is node(14), current = node(14), as current is not null, put it on to stack.

Again move to left child of current node (14), which is node(12). current = node(12) which is not null, put it onto stack.

Get left child of current node, which is null. So pop from stack, current = node(12). Process it. Traversal = [1,5,6,10,12]

Current node = current.right, i.e null, so pop out of stack. current = node(14). Process node(14). Traversal = [1,5,6,10,12,14]

Again current = current.right which is node(15). Put it back on to stack.

Left child of node(15) is null, so we pop from stack. current = node(15). Process node(15). Fetch right child of current node which is again null and this time even stack is already empty. So stop processing and everything is done. Traversal = [1,5,6,10,12,14,15]

### Iterative inorder traversal : Implementation

#include <stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define STACK_SIZE 10

typedef struct stack{
int top;
Node *items[STACK_SIZE];
}stack;

void push(stack *ms, Node *item){
if(ms->top < STACK_SIZE-1){
ms->items[++(ms->top)] = item;
}
else {
printf("Stack is full\n");
}
}

Node * pop (stack *ms){
if(ms->top > -1 ){
return ms->items[(ms->top)--];
}
else{
printf("Stack is empty\n");
}
}
Node * peek(stack ms){
if(ms.top < 0){
printf("Stack empty\n");
return 0;
}
return ms.items[ms.top];
}
int isEmpty(stack ms){
if(ms.top < 0) return 1;
else return 0;
}

void inorderTraversalWithoutStack(Node *root){
stack ms;
ms.top = -1;
Node *currentNode  = root;
while(!isEmpty(ms) || currentNode ){
if(currentNode){
push(&ms, currentNode);
currentNode = currentNode->left;
}
else {
currentNode = pop(&ms);
printf("%d  ", currentNode->value);
currentNode = currentNode->right;
}
}
}

void inorder (Node * root){
if ( !root ) return;

inorder(root->left);
printf("%d ", root->value );
inorder(root->right);
}

Node * createNode(int value){
Node * temp =  (Node *)malloc(sizeof(Node));
temp->value = value;
temp->right= NULL;
temp->left = NULL;
return temp;
}
Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree
root = addNode(root,30);
root = addNode(root,20);
root = addNode(root,15);
root = addNode(root,25);
root = addNode(root,40);
root = addNode(root,37);
root = addNode(root,45);
inorder(root);
printf("\n");
inorderTraversalWithoutStack(root);
return 0;
}

Complexity of iterative implementation of inorder traversal is O(n) with worst case space complexity of O(n).

Please share if there is something wrong or missing. If you want to contribute an share your knowledge with thousands of learners across the world, please reach out to communications@algorithmsandme.com

# Identical binary trees

Given two binary trees, check if two trees are identical binary trees? First question arises : when do you call two binary trees are identical? If root of two trees is equal and their left and right subtrees are also identical, then two trees are called as identical binary trees. For example, two trees below are identical.

whereas these two trees are not identical as node  6 and 8 differ as well as node 14 and 16.

## Identical binary trees : Thoughts

Solution to the problem lies in the definition of identical tree itself. We check if roots are equal, if there are not, there is no point continue down the tree, just return that trees are not identical.
If roots are equal, we have to check is subtrees are equal. To do, so take left subtrees of both original trees and validate if left subtrees are identical too. If not, return negative response. If yes, check if right subtrees are identical too.

Did you notice two things? First, that solution to original problem depends on solution of subtrees and at each node problem reduces itself to smaller subproblem.
Second,  processing order is preorder, we process roots of two trees, then left subtrees and at last right subtree. As Mirror binary search tree and Delete binary search tree are solved using postorder traversal, this problem can be solved using preorder traversal with special processing at root node.

We can also solve it by postorder traversal too, in that case, we will be unnecessary scanning subtrees even when roots themselves are not equal.

### Identical binary trees : example

Let’s take an example and see how it works. Start with root nodes, both roots are equal, move down to left subtree.

Left node in both subtrees is node(5), which is again equal. Go down the left subtree.

Again, left nodes in both subtrees is node(1), move down the left subtrees of both.

As per definition, two empty binary trees are identical. So when we move down the left child of nodes, they are null, hence identical and we return true to parent node(1). Same is true for right child.

We already know that at node(1), left and right subtree are identical, as well as node values are equal, we return true to parent node(5).

Left subtrees of node(5) are identical, move to right subtree to node(6). Similar to node(1), it also return true to parent node.

At node(5), left and right subtrees are identical, and also node values are equal, we return true to node(10).

Left subtrees are identical, now go to right subtree of node(10) in both trees.

Node(14) are equal in both trees, so check left subtrees of node(14) of both trees are identical. Both left subtree and right subtree of node(14) identical, same as node(1) and node(6) in left subtree, so they return true to parent node(14).

Now, at node(14), left and right subtrees are identical, so return true up to parent node(10).

Now, at  root node of both trees, there left subtrees and right subtrees are identical, so we return true for question if these two trees are identical or not.

Can you draw non identical binary trees and come up with flow and determine when they will be called out to be non-identical?

### Identical binary trees : Implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define true 1
#define false 0

void inoderTraversal(Node * root){
if(!root) return;

inoderTraversal(root->left);
printf("%d ", root->value);
inoderTraversal(root->right);
}

Node *createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node *addNode(Node *node, int value){
if(!node) return createNode(value);

if (node->value > value)
node->left = addNode(node->left, value);
else
node->right = addNode(node->right, value);

return node;
}

int isIdenticalBST( Node * firstTree, Node *secondTree){
if( ! (firstTree || secondTree ) ) //both of them are empty
return true;

if( !( firstTree && secondTree ) ) // one of them is empty
return false;

return ( firstTree->value == secondTree->value )
&& isIdenticalBST( firstTree->left, secondTree->left )
&& isIdenticalBST( firstTree->right, secondTree->right );
}

/* Driver program for the function written above */
int main(){
Node *firstRoot = NULL;

//Creating a binary tree
firstRoot = addNode(firstRoot, 30);
firstRoot = addNode(firstRoot, 20);
firstRoot = addNode(firstRoot, 15);
firstRoot = addNode(firstRoot, 25);
firstRoot = addNode(firstRoot, 40);
firstRoot = addNode(firstRoot, 38);
firstRoot = addNode(firstRoot, 45);

printf("Inorder traversal of tree is : ");
inoderTraversal(firstRoot);

printf("\n");

Node *secondRoot = NULL;

//Creating a binary tree
secondRoot = addNode(secondRoot, 30);
secondRoot = addNode(secondRoot, 20);
secondRoot = addNode(secondRoot, 15);
secondRoot = addNode(secondRoot, 25);
secondRoot = addNode(secondRoot, 40);
secondRoot = addNode(secondRoot, 38);
secondRoot = addNode(secondRoot, 45);

printf("Inorder traversal of tree is : ");
inoderTraversal(secondRoot);
printf("\n");

printf( "Two trees are identical : %s" ,
isIdenticalBST( firstRoot, secondRoot ) ? "True" :"false");

return 0;
}

Complexity to find if two trees are identical binary trees is O(n) where n is number of trees in smaller tree.

Please share if there is something wrong or missing. If you are willing to contribute and share your knowledge with thousands of learners across the world, please reach out to us communications@algorithmsandme.com

# Height of binary tree

One of the most basic problems on binary search tree is to find height of binary search tree or binary tree. First of all, what do we mean by height of binary search tree or height of binary tree? Height of tree is the maximum distance between the root node and any leaf node of the tree. For example, height of tree given below is 5, distance between node(10) and node(8). Note that we have multiple lea nodes, however we chose the node which s farthest from the root node.

## Height of binary tree : Thoughts

Brute force method to find height will be to calculate distance of each node from the root and take the maximum of it. As we will traversing each node of tree complexity will be O(n) and we need O(2logn) space to store distance for each leaf node.

What if we go bottom up instead of measuring distance of leaf nodes from root? What will be height of leaf node? At leaf node, there is no tree below it, hence height should be 1, which is node itself. What will be height of empty tree where root itself is null? It will be zero.
What if a node has a left subtree? Then height of subtree at that node will be height of left subtree + 1 (for the node itself). Same is true if node has only right subtree.

Interesting case is when node has both left and right subtree. Which height we should take to get height of subtree at node? As we are looking for maximum distance, we should take maximum of both subtrees and add 1 to get height at that node.

As we are going bottom up and building the height up from leaf to node, it is necessary to pass on height of left subtree and right subtree to root node. It means we have to process subtrees before root node. What kind of traversal is it? As in Delete binary search tree and Mirror binary search tree this problem is also postorder traversal of binary tree with specific processing at root node.

Let’s take and example and see how this method works? Given below binary tree,find height of it.

We have to start from bottom and for that follow the path till node is current node is null. At root node(10), is it node null? No, then move down the left subtree.

Is node(5) null? Nope, again move down to left subtree.

At node(4), it is not null, hence we move down to left subtree. But as left child of node(4) is null, it we will return 0 as height of an empty binary tree should be 0. Again, node(4) does not even have right child, so from right side too it gets a zero. What will be height of node(40) then? Max(0,0 ) + 1 = 1, which it returns back to parent node(5).

Back at node(5), we go the height of left subtree, there is right subtree too, so we will find height of it, before judging the height of subtree at node(5). So move down the right side of node(5).

Node(7) is not null, so move down the left subtree of it, which is node(6).

Node(6) is also not null, hence we move down the left subtree which is null. Null subtree returns 0. It is same for right subtree of node(6). So, node(6) return max(0,0) + 1 = 1 to parent node.

Back at node(7), there is right subtree too, so move down it to node(9).

As node(9) is not null, move down the left child which is node(8).

We move left of node(8) which is null and even right subtree is null, as all leaf node, it also return 1 to parent node.

At node(9), right child is null which return 0. So what should be height of node(9)? It will be max(1,0) + 1 = 2. 1 is height of left subtree and 0 is height of right subtree.
In the same vein, node(7) will return 3 to node(5).

At node(5), return max(1,3) +1 = 4.

Now, at node(10), we have height of left subtree let’s calculate height of right subtree. Move down to node(14).

Node(14) not null, move to left subtree to node(12).

As node(12) is not null, move to left side, which being null, return 0. Similarly for right child, it also returns 0. So, node(12) return max(0,0) +1 to parent node.

Move down to right subtree of node(14) to node(15).

As explained other cases, node(15) too will return 1.

At this point, we have height of left subtree and right subtree of node(14), hence return height  = max(1,1) + 1 = 2 to parent node.

Here, at node(10), we have height of left and right subtrees. What will be height of the binary tree then? it will be max(4,2) + 1 = 5.

Hope this example clarifies how recursive bottom up approach works to find height of binary tree.

### Height of binary tree : Implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define MAX(a,b)  (a < b ? b : a)

Node *createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node *addNode(Node *node, int value){
if(!node) return createNode(value);

if (node->value > value)
node->left = addNode(node->left, value);
else
node->right = addNode(node->right, value);

return node;
}

int height( Node *root){
if( !root ) return 0;

int lheight = height( root->left);
int rheight = height( root->right);

return 1 + MAX (lheight, rheight );
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;

//Creating a binary tree
root = addNode(root, 30);
root = addNode(root, 20);
root = addNode(root, 15);
root = addNode(root, 25);
root = addNode(root, 40);
root = addNode(root, 38);
root = addNode(root, 39);
root = addNode(root, 45);

printf( "Height of tree is : %d", height( root));

return 0;
}

Complexity of recursive method is O(n) as we will b scanning all nodes at least once. Be aware of drawback of recursive nature of this implementation as it may lead of stack overflow in production environments.

Two important things we learn from this problem : First how to traverse a tree which is part of solution to many of binary tree problems. Second, how to return values for subtrees to root and process those values at root node. We saw same thing happening in Replace node with sum of children in BST.

Please share if there is something wrong or missing. If you want to contribute and share your knowledge with thousands of learners across world, please reach out to us at communications@algorithmsandme.com

# Balanced binary tree

What is a balanced binary tree?  A tree is balanced when difference between height of left subtree and right subtree at every node is not more than one. Problem is to check if given binary tree is balanced tree? For example, below tree is balanced tree because at all nodes difference between height of left and right subtree is not more than 1.

However, below tree is not balanced as difference between height of left subtree and right subtree at node(10) is 2.

## Balanced binary tree : Thoughts

One thing to notice very carefully, that for a tree to be balanced, every subtree at each node should be balanced too. It cannot be balanced if it is balanced at root node.

As solution to original tree depends on subtree, we have to find first if subtrees are balanced. If yes, then compare the height of left subtree and right subtree, if difference is less than or equal to 1, return true. Refer Height of binary tree to learn how to find height of binary tree.

Let’s see how does it work with an example. Given below binary tree and see how we can figure out if it is balanced or not?

Start with root node which is node(10). Height of left subtree is 4 and right subtree is 3. Difference of heights is 1, now we have to check if it’s left and right subtrees are balanced?

At node(5), again height difference is 1, so we will go down left and right subtrees and check if they are balanced.

At node(1), it’s leaf node and hence, it must be balanced. So we return true to parent node(5).

Now, we have to check if right subtree of node(5) is balanced too? We move down right subtree and go all the way to node(6), which is leaf node, which returns true to node(8). Also, node(9) will return true. At node(8), both left and right subtree have returned true, we should return true from node(8) too.

At node(5), left and subtree have returned true and we know that difference between height of left and right subtree is only 1. Hence we will return true from node(5) too.

Next, we have to check if right subtree of node(10) is balanced too.

Left and right subtree of node(19) are leaf nodes, hence both will return true.

At node(19), left and right subtree has returned true and height difference is zero, hence it will return true to parent node.

Now, at last, we have received that left and right subtrees are balanced at node(10) and height difference is 1, hence, entire tree is balanced.

### Balanced binary tree : Implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define true 1
#define false 0
#define MAX(a,b)  (a < b ? b : a)

Node *createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));
newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}
Node *addNode(Node *node, int value){
if(!node)
return createNode(value);

if (node->value > value)
node->left = addNode(node->left, value);
else
node->right = addNode(node->right, value);

return node;
}

int height( Node *root){
if( !root )
return 0;

int lheight = height( root->left);
int rheight = height( root->right);

return 1 + MAX (lheight, rheight );
}

int isHeightBalanced( Node * root ){
if(!root) return true;

int lheight = height( root->left );
int rheight = height( root->right );

return (abs( lheight-rheight) <= 1 )
&& isHeightBalanced( root->left )
&& isHeightBalanced( root->right );

}
/* Driver program for the function written above */
int main(){
Node *root = NULL;

//Creating a binary tree
root = addNode(root, 30);
root = addNode(root, 20);
root = addNode(root, 15);
root = addNode(root, 25);
root = addNode(root, 40);
root = addNode(root, 38);
root = addNode(root, 39);
root = addNode(root, 45);
root = addNode(root, 47);
root = addNode(root, 49);

printf( "Is tree height balanced : %s",
isHeightBalanced( root ) ? "Yes" : "No" );

return 0;
}

Complexity of algorithm to find if tree is balanced or not is O(n) as we will be scanning all nodes at least once. However, if you notice that we are scanning tree multiple times when calculating height of left and subtree at every node. How can we avoid these repeated scans?

As we are already scanning all nodes, can we pass the two things from subtree to root node? If we can pass from subtree if subtree is balanced or not and then what is height of subtree. Idea is to bump height all the way to root node and use that height to check if tree at root is balanced or not.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

#define true 1
#define false 0
#define MAX(a,b)  (a < b ? b : a)

Node *createNode(int value){
Node * newNode =  (Node *)malloc(sizeof(Node));
newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}
Node *addNode(Node *node, int value){
if(!node)
return createNode(value);

if (node->value > value)
node->left = addNode(node->left, value);
else
node->right = addNode(node->right, value);

return node;
}

int isHeightBalanced( Node * root, int *height ){
if(!root){
*height = 0;
return true;
}

int lheight = 0, rheight = 0;
int lBalanced = isHeightBalanced( root->left, &lheight );
int rBalanced = isHeightBalanced( root->right, & rheight );

//Update the height
*height = 1 + MAX ( lheight, rheight);

//Check if difference between two height is more than 1
if (abs( lheight-rheight) > 1 ) return false;

return lBalanced && rBalanced;

}
/* Driver program for the function written above */
int main(){
Node *root = NULL;

//Creating a binary tree
root = addNode(root, 30);
root = addNode(root, 20);
root = addNode(root, 15);
root = addNode(root, 25);
root = addNode(root, 40);
root = addNode(root, 38);
root = addNode(root, 39);
root = addNode(root, 45);
root = addNode(root, 47);
root = addNode(root, 49);

printf( "Is tree height balanced : %s",
isHeightBalanced( root ) ? "Yes" : "No" );

return 0;
}

Complexity of this implementation is also O(n) however, number of scans of each node is only once.

Please share if there is something is wrong or missing. If you want to contribute and share your knowledge with thousands of learners across the world, please reach out to us at communications@algorithmsandme.com