Skip to content

Instantly share code, notes, and snippets.

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;
@Ray1988
Ray1988 / gist:5146396
Created March 12, 2013 19:54
4.4 given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth
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)
@Ray1988
Ray1988 / gist:5164814
Created March 14, 2013 20:14
check a binary tree is a binary search tree
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)
@Ray1988
Ray1988 / gist:5164891
Created March 14, 2013 20:23
Check is a binary tree is binary search tree(improved)
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;
@Ray1988
Ray1988 / gist:5165605
Created March 14, 2013 21:54
find the next node of given node in binary search tree.
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;
@Ray1988
Ray1988 / gist:5166439
Created March 15, 2013 00:09
common ancester of two treeNodes
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);
@Ray1988
Ray1988 / gist:5171157
Last active December 15, 2015 00:09
decide if T2 is subTree of T1
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){
@Ray1988
Ray1988 / gist:5171567
Created March 15, 2013 17:26
given a tree, print out the path which contain together is a given number.
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;
@Ray1988
Ray1988 / gist:5172511
Created March 15, 2013 19:36
merge sorted array b in sorted array a, assume a has enough space to hold b in the end.
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--;
@Ray1988
Ray1988 / gist:5172675
Created March 15, 2013 19:58
sorted a string array base on anagram.
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));