Posts

LCA(Lowest Common Ancestor) of binary tree - JAVA

public class LowestCommonAncestor { static class TreeNode{ private int data ; private TreeNode leftNode ; private TreeNode rightNode ; public TreeNode( int data) { this . data = data ; } public int getData () { return data ; } public void setData ( int data) { this . data = data ; } public TreeNode getLeftNode () { return leftNode ; } public void setLeftNode (TreeNode leftNode) { this . leftNode = leftNode ; } public TreeNode getRightNode () { return rightNode ; } public void setRightNode (TreeNode rightNode) { this . rightNode = rightNode ; } } public static TreeNode lca (TreeNode root , TreeNode x , TreeNode y){ if (root == null ) return null; if (root.getData() == x.getData() || root.getData() == y.getData()) ...

Segregate 0's and 1's in an Array - Java

public class Sort0s1s { public static void main (String[] args) { int [] inAr = { 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 1 } ; int [] ouAr = Sort0s1s. sort0s1s (inAr) ; for ( int i = 0 ; i < ouAr. length ; i++) { System. out .println(ouAr[i]) ; } } public static int [] sort0s1s ( int [] inAr){ int i = 0 ; int j = inAr. length - 1 ; while (i<j){ if (i < inAr. length - 1 && inAr[i] == 0 ){ i++ ; } if (j > 0 && inAr[j] == 1 ){ j-- ; } else { int temp = inAr[i] ; inAr[i] = inAr[j] ; inAr[j] = temp ; } } return inAr ; } } O/P: 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1

Linked list - Reverse without recursion - java

public class Reverse { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); Node n6 = new Node(6); n1.setNext(n2); n2.setNext(n3); n3.setNext(n4); n4.setNext(n5); n5.setNext(n6); n6.setNext(null); Node nth = reverse(n1); System.out.println(nth.getData()); } private static Node reverse(Node head){ Node temp = null; Node nextNode = null; while(head != null){ nextNode = head.getNext(); head.setNext(temp); temp = head; head = nextNode; } return temp; } } ----------------------------------------------- o/p: 6

Linked list - If loop exists get starting of loop - Java

public class Cirsle { /** * @param args */ public static void main(String[] args) { Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); Node n6 = new Node(6); n1.setNext(n2); n2.setNext(n3); n3.setNext(n4); n4.setNext(n5); n5.setNext(n6); n6.setNext(n3); Node nth = circle(n1); System.out.println(nth.getData()); } private static Node circle(Node root){ Node slwPtr = null; Node fasPtr = null; boolean loopExists = false; if(root != null){ slwPtr = root; fasPtr = slwPtr; } while(fasPtr.getNext() != null && fasPtr.getNext().getNext() != null){ slwPtr = slwPtr.getNext(); fasPtr = fasPtr.getNext().getNext(); if(slwPtr == fasPtr){ loopExists = true; break;    } } if(loopExists){ slwPtr = root; while(slwPtr != fasPtr){ slwPtr = slwPtr.getNext(); fasPtr = fasPtr.getNext(); ...

Find pair whose sum is k - java

public class PairSum {     public static void main(String[] args) {         pair(new int[]{1,2,4,5,6},6);     }         private static void pair(int[] nums, int k){         int i = 0;         int j = nums.length -1;         while(i < j){             if(nums[i] + nums[j] == k){                 System.out.println(nums[i] +"--------"+nums[j]);                 i++;                 j--;             }else if(nums[i] + nums[j] > k)                 j--;    ...

Sum of all nodes in a tree - java

package trees; public class SumOfNodes {     public static void main(String[] args) {         Node rootnode = new Node(25);           System.out.println("Building tree with rootvalue " + rootnode.getNodeValue());           System.out.println("================================");           rootnode.insert(rootnode, 11);           rootnode.insert(rootnode, 15);           rootnode.insert(rootnode, 16);           rootnode.insert(rootnode, 23);           rootnode.insert(rootnode, 79);           System.out.println("Total Count");           System.out.println("=================================");      ...

Height of a binay tree - java

package trees; public class Height {     public static void main(String[] args) {         Node rootnode = new Node(25);           System.out.println("Building tree with rootvalue " + rootnode.getNodeValue());           System.out.println("================================");           rootnode.insert(rootnode, 11);           rootnode.insert(rootnode, 15);           rootnode.insert(rootnode, 16);           rootnode.insert(rootnode, 23);           rootnode.insert(rootnode, 79);           System.out.println("Height");           System.out.println("=================================");         ...