Trees
By: Tannishk Sharma
Tags: Binary Tree, Programming, Coding Interviews

These days i am obsessed with trees .Listen to me when I tell you with the proper understanding of recursion tree becomes easy really really easy . Tree is one of the most commonly used data structure for solving computer science problems .

We are going to solve all commonly asked interview problems based on tree this is the part 1 of the series rest will follow Enjoy

Identical Binary Trees

A very common problem is to determine whether 2 trees which are given to you are equal or not

My Java Solution :

public class Solution {

public int isSameTree(TreeNode A, TreeNode B) {

if(A==null && B==null)

return 1;

if(A!=null && B!=null){

if ((A.val == B.val) &&

(isSameTree(A.left,B.left)==1) &&

(isSameTree(A.right,B.right)==1)){

return 1;

}

}

return 0;

}

}

Symmetric Binary Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

Example :

1

/ \

2 2

/ \ / \

3 4 4 3

The above binary tree is symmetric.

But the following is not:

1

/ \

2 2

\ \

3 3

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

My Java based Solution :

public class Solution {

public boolean isSymmetricHelp(TreeNode left,TreeNode right){

if(left == null && right == null){

return true;

}

if(left!=null && right !=null){

return (left.val) == (right.val) &&

isSymmetricHelp(left.left,right.right)&&

isSymmetricHelp(left.right,right.left);

}

return false;

}

public int isSymmetric(TreeNode A) {

if(A==null)

return 1;

if(isSymmetricHelp(A.left,A.right)){

return 1;

} else {

return 0;

}

}

}

Finding Lowest Common Ancestor

Lowest common ancestor : the lowest common ancestor (LCA) of two nodes v and w in a tree or directed acyclic graph (DAG) is the lowest (i.e. deepest) node that has both v and w as descendants.

My Java Based Solution :

public class Solution {

public int lca(TreeNode a, int val1, int val2) {



a = findLCA(a, val1, val2);

if(a == null)

return -1;

return a.val;



} public TreeNode findLCA(TreeNode node, int val1, int val2){

if( node == null)

return null;

if(node.val == val1 || node.val == val2)

return node;



TreeNode leftLCA = findLCA(node.left, val1, val2);

TreeNode rightLCA = findLCA(node.right, val1, val2);



if(leftLCA != null && rightLCA != null)

return node;



return (leftLCA != null)? leftLCA: rightLCA;



}

}

Invert the Binary Tree

Given a binary tree, invert the binary tree and return it.

Look at the example for more details.

Example :

Given binary tree

1

/ \

2 3

/ \ / \

4 5 6 7

invert and return

1

/ \

3 2

/ \ / \

7 6 5 4

My Java Based Solution

public class Solution {

public TreeNode invertTree(TreeNode A) {

swap(A);

return A;

}



public void swap(TreeNode A){

if(A==null)

return;



TreeNode temp = A.left;

A.left = A.right;

A.right = temp;



if(A.left!=null)

swap(A.left);



if(A.right!=null)

swap(A.right);

}

}

Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

Height-balanced binary tree : is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

A hint : Ensure Left subtree is balanced , Right subtree is balanced and difference of height between them is +-1 .

My Java Based Solution :

public class Solution {

public int heightOfTree(TreeNode A){

if(A==null)

return 0;

int leftHeight = heightOfTree(A.left);

int rightHeight = heightOfTree(A.right);

return 1 + Math.max(leftHeight,rightHeight);

}

public int isBalanced(TreeNode A) {

if(A==null)

return 1;

int leftHeight = heightOfTree(A.left);

int rightHeight = heightOfTree(A.right);

if(Math.abs(rightHeight - leftHeight) <= 1 &&

(isBalanced(A.left) == 1) &&

(isBalanced(A.right) == 1)

)

return 1;

else

return 0;

}

}

Max Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth of a binary tree is the number of nodes along the longest path from the root node down to the farthest leaf node.

public class Solution {

public int maxDepth(TreeNode A) {

if(A==null)

return 0;

return 1+Math.max(maxDepth(A.left),maxDepth(A.right));

}

}

Sorted Array To Balanced BST

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Approach Hint : Try to find the median of the array make it the root and recursively do this for both left and right subtree

public class Solution {

public TreeNode sortedArrayToBST(final List<Integer> a) {

return helper(a,0,a.size()-1);

}



public TreeNode helper(final List<Integer> a,int start,int end){

if(start<=end){

int mid = (start+end)/2;

TreeNode root = new TreeNode(a.get(mid));

root.left = helper(a,start,mid-1);

root.right = helper(a,mid+1,end);

return root;

} else {

return null;

}

}

}

Kth Smallest Element In Tree

Given a binary search tree, write a function to find the kth smallest element in the tree.