2012年10月21日星期日

Construct Binary Tree from Preorder and Inorder Traversal

public TreeNode buildTree(int[] preorder, int[] inorder) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(preorder == null|| preorder.length==0 || inorder == null) return null;
        int l1 =0;
        int l2 =0;
        int h1=preorder.length-1;
        int h2=inorder.length-1;
        return buildTreeT(preorder, l1, h1, inorder,l2, h2);
    }
   
    public TreeNode buildTreeT(int[] preorder, int l1, int h1, int[] inorder, int l2, int h2){
       
        int index1=0;
        int index2=0;
        if(l1>h1||l2>h2) return null;       
       
        for(int i =l2; i<=h2;i++){
            if(inorder[i] == preorder[l1]){
                index2=i;
            }
        }
       
        index1 = index2-l2+l1;
       
        TreeNode head = new TreeNode(preorder[l1]);
        head.left=buildTreeT(preorder, l1+1, index1, inorder, l2,index2-1);
        head.right=buildTreeT(preorder, index1+1, h1, inorder,index2+1,h2);
        return head;
    }

Climbing Stairs

public int climbStairs(int n) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(n<=0) return 0;
  
        int array[] = new int[n+1];
        array[1]=1;
        array[0]=1;
        for(int i=2;i<=n;i++){
            array[i]=array[i-1]+array[i-2];
        }
       
        return array[n];
    }

Binary Tree Zigzag Level Order Traversal

public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        LinkedList<TreeNode> pre = new LinkedList<TreeNode>();
        if(root ==null) return result;
        boolean flag =true;
        pre.add(root);
        while(!pre.isEmpty()){
           TreeNode tmp=null;
           LinkedList<TreeNode> current = new LinkedList<TreeNode>();
           ArrayList<Integer> level = new ArrayList<Integer>();
           while(!pre.isEmpty()){
               if(flag == true){
                tmp = pre.removeFirst();
                if(tmp.left!=null)current.addLast(tmp.left);
                if(tmp.right!=null)current.addLast(tmp.right);
               }else{
                   tmp = pre.removeLast();
                   if(tmp.right!=null)current.addFirst(tmp.right);
                if(tmp.left!=null)current.addFirst(tmp.left);
               }
                level.add(tmp.val);             
           }
           flag = flag^true;
           result.add(level);
           pre = current;
        }       
        return result;
    }

2012年10月14日星期日

Maximum Depth of Binary Tree

 public int maxDepth(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(root ==null) return 0;
       
        return max(maxDepth(root.left) +1, maxDepth(root.right)+1);
    }
   
    public int max(int val1, int val2){
        return val1>val2? val1:val2;
    }

Same Tree

public boolean isSameTree(TreeNode p, TreeNode q) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(p == null && q == null) return true;
        if(p == null || q == null) return false;
       
        if(p.val == q.val){
            return isSameTree(p.left, q.left) &&
                   isSameTree(p.right, q.right);    
        }
        return false;
    }

Symmetric Tree

   // Start typing your Java solution below
        // DO NOT write main() function
        if(root == null || root.left==null && root.right==null) return true;
       
        if(root.left!=null && root.right!=null){
            if(root.left.val == root.right.val){
                return checkTree(root.left.right, root.right.left) &&
                checkTree(root.left.left, root.right.right);
            }
        }
        return false;       
    }
   
    boolean checkTree(TreeNode first, TreeNode second){
        if(first==null && second ==null) return true;
        if(first==null || second == null) return false;
        if(first.val !=second.val) return false;
       
        return checkTree(first.left, second.right) &&
               checkTree(first.right, second.left);
    }