Skip to content

Commit e00fe32

Browse files
Add files via upload
1 parent 5dcb6f6 commit e00fe32

9 files changed

+371
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public class Solution {
2+
3+
public static BinaryTreeNode<Integer> buildTree(int[] postOrder, int[] inOrder) {
4+
//Your code goes here
5+
BinaryTreeNode<Integer> root = buildTree(postOrder, inOrder, 0 ,postOrder.length-1, 0, inOrder.length-1);
6+
return root;
7+
}
8+
9+
public static BinaryTreeNode<Integer> buildTree(int[] postOrder, int[] inOrder,int siPost, int eiPost, int siIn, int eiIn) {
10+
// TODO Auto-generated method stub
11+
12+
//Base case - If number of elements in the post-order is 0
13+
if (siPost>eiPost)
14+
{
15+
return null;
16+
}
17+
18+
//Defining the root node for current recursion
19+
int rootData=postOrder[eiPost];
20+
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
21+
22+
//Finding root data's location in Inorder (Assuming root data exists in Inorder)
23+
int rootIndex=-1;
24+
for (int i=siIn;i<=eiIn;i++)
25+
{
26+
if (rootData==inOrder[i])
27+
{
28+
rootIndex=i;
29+
break;
30+
}
31+
}
32+
33+
//Defining index limits for Left Subtree Inorder
34+
int siInLeft=siIn;
35+
int eiInLeft=rootIndex-1;
36+
37+
//Defining the index limits for Left Subtree Preorder
38+
int siPostLeft=siPost;
39+
int leftSubTreeLength = eiInLeft - siInLeft + 1;
40+
int eiPostLeft=(siPostLeft)+(leftSubTreeLength-1);
41+
42+
//Defining index limits for Right Subtree Inorder
43+
int siInRight=rootIndex+1;
44+
int eiInRight=eiIn;
45+
46+
//Defining index limits for Right Subtree Preorder
47+
int siPostRight=eiPostLeft+1;
48+
int eiPostRight=eiPost-1;
49+
50+
BinaryTreeNode<Integer> rightChild = buildTree(postOrder, inOrder, siPostRight, eiPostRight, siInRight, eiInRight);
51+
BinaryTreeNode<Integer> leftChild = buildTree(postOrder, inOrder, siPostLeft, eiPostLeft, siInLeft, eiInLeft);
52+
53+
root.left=leftChild;
54+
root.right=rightChild;
55+
return root;
56+
}
57+
58+
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
public class Solution {
2+
3+
public static BinaryTreeNode<Integer> buildTree(int[] preOrder, int[] inOrder) {
4+
//Your code goes here
5+
BinaryTreeNode<Integer> root = buildTree(preOrder, inOrder, 0 ,preOrder.length-1, 0, inOrder.length-1);
6+
return root;
7+
8+
}
9+
10+
public static BinaryTreeNode<Integer> buildTree(int[] preorder, int[] inorder,int siPre, int eiPre, int siIn, int eiIn)
11+
{
12+
//Base case - If number of elements in the pre-order is 0
13+
if (siPre>eiPre)
14+
{
15+
return null;
16+
}
17+
18+
//Defining the root node for current recursion
19+
int rootData=preorder[siPre];
20+
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
21+
22+
//Finding root data's location in Inorder (Assuming root data exists in Inorder)
23+
int rootIndexInorder=-1;
24+
for (int i=siIn;i<=eiIn;i++)
25+
{
26+
if (rootData==inorder[i])
27+
{
28+
rootIndexInorder=i;
29+
break;
30+
}
31+
}
32+
33+
//Defining index limits for Left Subtree Inorder
34+
int siInLeft=siIn;
35+
int eiInLeft=rootIndexInorder-1;
36+
37+
//Defining the index limits for Left Subtree Preorder
38+
int siPreLeft=siPre+1;
39+
int leftSubTreeLength = eiInLeft - siInLeft + 1;
40+
int eiPreLeft=(siPreLeft)+(leftSubTreeLength-1);
41+
42+
//Defining index limits for Right Subtree Inorder
43+
int siInRight=rootIndexInorder+1;
44+
int eiInRight=eiIn;
45+
46+
//Defining index limits for Right Subtree Preorder
47+
int siPreRight=eiPreLeft+1;
48+
int eiPreRight=eiPre;
49+
50+
BinaryTreeNode<Integer> leftChild = buildTree(preorder, inorder, siPreLeft, eiPreLeft, siInLeft, eiInLeft);
51+
BinaryTreeNode<Integer> rightChild = buildTree(preorder, inorder, siPreRight, eiPreRight, siInRight, eiInRight);
52+
root.left=leftChild;
53+
root.right=rightChild;
54+
return root;
55+
}
56+
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
3+
public static void insertDuplicateNode(BinaryTreeNode<Integer> root) {
4+
//Your code goes here
5+
if (root==null)
6+
return;
7+
8+
BinaryTreeNode<Integer> duplicateNode = new BinaryTreeNode<Integer>(root.data);
9+
//duplicateNode.data=root.data;
10+
BinaryTreeNode<Integer> temp=root.left;
11+
root.left=duplicateNode;
12+
duplicateNode.left=temp;
13+
insertDuplicateNode(root.left.left);
14+
insertDuplicateNode(root.right);
15+
16+
17+
18+
}
19+
20+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
3+
public static int diameterOfBinaryTree(BinaryTreeNode<Integer> root){
4+
//Your code goes here
5+
if (root==null)
6+
{
7+
return 0;
8+
}
9+
return findHeight(root.left)+findHeight(root.right)+1;
10+
}
11+
12+
public static int findHeight(BinaryTreeNode<Integer> root)
13+
{
14+
if (root==null)
15+
{
16+
return 0;
17+
}
18+
int leftHeight=findHeight(root.left);
19+
int rightHeight=findHeight(root.right);
20+
21+
if(leftHeight>rightHeight)
22+
{
23+
return leftHeight+1;
24+
}
25+
else
26+
{
27+
return rightHeight+1;
28+
}
29+
}
30+
31+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
public class Solution {
3+
4+
public static void printLevelWise(BinaryTreeNode<Integer> root) {
5+
//Your code goes here
6+
if (root==null)
7+
return;
8+
9+
Queue<BinaryTreeNode<Integer>> nodesToPrint = new LinkedList<BinaryTreeNode<Integer>>();
10+
nodesToPrint.add(root);
11+
nodesToPrint.add(null);
12+
while(!nodesToPrint.isEmpty())
13+
{
14+
BinaryTreeNode<Integer> front=nodesToPrint.poll();
15+
if (front==null)
16+
{
17+
if (nodesToPrint.isEmpty())
18+
break;
19+
else
20+
{
21+
System.out.println();
22+
nodesToPrint.add(null);
23+
}
24+
25+
}
26+
else
27+
{
28+
System.out.print(front.data+" ");
29+
if (front.left!=null)
30+
nodesToPrint.add(front.left);
31+
if (front.right!=null)
32+
nodesToPrint.add(front.right);
33+
}
34+
}
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
3+
private static Pair<Integer,Integer> maxMin=new Pair<Integer,Integer>(Integer.MAX_VALUE,Integer.MIN_VALUE);
4+
5+
public static Pair<Integer,Integer> getMinAndMax(BinaryTreeNode<Integer> root) {
6+
//Your code goes here
7+
getMinAndMaxHelper(root);
8+
return maxMin;
9+
10+
11+
}
12+
13+
private static void getMinAndMaxHelper(BinaryTreeNode<Integer> root)
14+
{
15+
if (root==null)
16+
{
17+
return;
18+
}
19+
20+
int rootData=root.data;
21+
int maxVal=maxMin.maximum;
22+
if (rootData>maxVal)
23+
{
24+
maxMin.maximum=root.data;
25+
}
26+
27+
int minVal=maxMin.minimum;
28+
if (rootData<minVal)
29+
{
30+
maxMin.minimum=root.data;
31+
}
32+
getMinAndMaxHelper(root.left);
33+
getMinAndMaxHelper(root.right);
34+
}
35+
36+
}

BINARY TREE 2/MIRROR BINARY TREE.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
3+
public static void mirrorBinaryTree(BinaryTreeNode<Integer> root){
4+
//Your code goes here
5+
if (root==null)
6+
{
7+
return;
8+
}
9+
10+
BinaryTreeNode<Integer> temp=root.left;
11+
root.left=root.right;
12+
root.right=temp;
13+
mirrorBinaryTree(root.left);
14+
mirrorBinaryTree(root.right);
15+
}
16+
17+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.*;
2+
/*
3+
4+
Following is the structure used to represent the Binary Tree Node
5+
6+
class BinaryTreeNode<T> {
7+
T data;
8+
BinaryTreeNode<T> left;
9+
BinaryTreeNode<T> right;
10+
11+
public BinaryTreeNode(T data) {
12+
this.data = data;
13+
this.left = null;
14+
this.right = null;
15+
}
16+
}
17+
18+
*/
19+
20+
public class Solution {
21+
22+
public static void rootToLeafPathsSumToK(BinaryTreeNode<Integer> root, int k) {
23+
//Your code goes here
24+
String arr="";
25+
rootToLeafPathsSumToK(root,k,arr);
26+
}
27+
28+
public static void rootToLeafPathsSumToK(BinaryTreeNode<Integer> root, int k,String arr)
29+
{
30+
if (root==null)
31+
{
32+
return;
33+
}
34+
35+
int rootData=root.data;
36+
//System.out.println("Root data: "+rootData);
37+
//System.out.println("k: "+k);
38+
//System.out.println("Old Arraylist: "+arr);
39+
arr=arr+rootData+" ";
40+
if(k==rootData && root.left==null && root.right==null)
41+
{
42+
//System.out.print("Path found: ");
43+
//for (int i=0;i<arr.length();i++)
44+
//System.out.print(arr.charAt(i)+" ");
45+
//System.out.println();
46+
System.out.println(arr);
47+
return;
48+
}
49+
//System.out.println();
50+
51+
rootToLeafPathsSumToK(root.left,k-rootData,arr);
52+
rootToLeafPathsSumToK(root.right,k-rootData,arr);
53+
}
54+
55+
}

BINARY TREE 2/PRINT LEVELWISE.txt

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
/*
3+
4+
Following is the structure used to represent the Binary Tree Node
5+
6+
class BinaryTreeNode<T> {
7+
T data;
8+
BinaryTreeNode<T> left;
9+
BinaryTreeNode<T> right;
10+
11+
public BinaryTreeNode(T data) {
12+
this.data = data;
13+
this.left = null;
14+
this.right = null;
15+
}
16+
}
17+
18+
*/
19+
20+
public class Solution {
21+
22+
public static void printLevelWise(BinaryTreeNode<Integer> root) {
23+
//Your code goes here
24+
if (root==null)
25+
{
26+
return;
27+
}
28+
Queue<BinaryTreeNode<Integer>> nodesToPrint=new LinkedList<BinaryTreeNode<Integer>>();
29+
nodesToPrint.add(root);
30+
while(!nodesToPrint.isEmpty())
31+
{
32+
BinaryTreeNode<Integer> front = nodesToPrint.poll();
33+
System.out.print(front.data+":");
34+
if (front.left!=null)
35+
{
36+
nodesToPrint.add(front.left);
37+
System.out.print("L:"+front.left.data);
38+
}
39+
else
40+
{
41+
System.out.print("L:-1");
42+
}
43+
44+
if (front.right!=null)
45+
{
46+
nodesToPrint.add(front.right);
47+
System.out.print(",R:"+front.right.data);
48+
}
49+
else
50+
{
51+
System.out.print(",R:-1");
52+
}
53+
System.out.println();
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)