This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TreeNode createminiBST(int arr[], int left, int righ){ | |
if(left> right) return null; | |
int mid= (left+right)/2; | |
TreeNode r=new TreeNode(arr[mid]); | |
r.left=createminiBST(arr, left, mid-1); | |
r.right=createminiBST(arr,mid, righ); | |
return r; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void createLinkedListFromBT(TreeNode r,ArrayList al, int level){ | |
if(r==null) return; | |
LinkedList<TreeNode> temp=null; | |
if(al.size()==level){ | |
temp=new LinkedList<TreeNode>(); | |
al.add(temp); | |
}else if(level<al.size()){ | |
temp=al.get(level) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean checkBinarySearchTree(TreeNode r){ | |
if(r==null) return true; | |
ArrayList<Integer> al=new ArrayList(); | |
Arraylist<Integer> arraylist=inOrderCreateArray(r, al); | |
return sortedArrayList(arraylist); | |
} | |
public ArrayList inOrderCreateArray(TreeNode r, ArrayList<Integer> al){ | |
if (r!=null){ | |
if(r.left!=null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int lastNum=Integer.miniValue(); | |
public boolean checkBST(TreeNode r){ | |
if(r==null) return true; | |
if(!checkBST(r.left)) return false; | |
if(r.data<lastNum) return false; | |
lastNum=r.data; | |
if(!checkBST(r.right) return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TreeNode findNextNode(TreeNode r){ | |
if (r==null) rerturn null; | |
if(r.father==null||r.right!=null){ | |
return leftMost(r.right); | |
} | |
else{ | |
TreeNode x=n; | |
TreeNode f=n.father; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TreeNode findCommonAnces(TreeNode r, TreeNode a, TreeNode b){ | |
if(r==null) return null; | |
if(r==a||r==b) return r; | |
boolean leftCover_a=cover(r.left, a); | |
boolean leftCover_b=cover(r.left, b); | |
if(leftCover_a!=leftCover_b) return r; | |
TreeNode child=leftCover_a?r.left:r.right | |
TreeNode common=findCommonAnces(child, a, b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean contain(TreeNode t1, TreeNode t2){ | |
if(t2==null) return true// null tree is alway a subtree | |
return subTree(t1,t2) | |
} | |
public boolean subTree(TreeNode t1, TreeNode t2){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void findSum(TreeNode r, int sum){ | |
int depth= depth(r); | |
int[] path=new int[depth]; | |
int level=0; | |
findSum(r, path, sum, level); | |
} | |
public void findSum(TreeNode r, int[] path, int sum, int level){ | |
if(r==null) return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] merge(int[] a, int[] b){ | |
int aEnd=a.length-1; | |
int bEnd=b.length-1; | |
int mergedEnd=aEnd+bEnd+1; | |
while(aEnd>=0&&bEnd>=0){ | |
if(a[aEnd]=b[bEnd]){ | |
a[mergeEnd]=a[aEnd]; | |
aEnd--; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnagramComparator implement Comparator<String>{ | |
public String sort(String s){ | |
char[] c= s.toCharArray() | |
Arrays.sort(c); | |
return new String(c); | |
} | |
public int compare(String s1, String s2){ | |
return sort(s1).compareTo(sort(s2)); |
OlderNewer