leetcode-探索二叉木

61201 ワード

1.        
/*
BFS
    :1 ms,     Java       91.06%   
    :40 MB,     Java       5.71%   
*/
class Solution {
     
 public List<List<Integer>> levelOrder(TreeNode root) {
     
        List<List<Integer>> res = new LinkedList<>();
        if (root==null) return res;
        Queue<TreeNode> tree = new LinkedList<>();
        tree.add(root);
        while (!tree.isEmpty()){
     
            int size = tree.size();
            List<Integer> ans = new LinkedList<>();
            for (int i = 0;i<size;i++){
     
                TreeNode temp = tree.remove();
                ans.add(temp.val);
                if (temp.left!=null) tree.add(temp.left);
                if (temp.right!=null) tree.add(temp.right);
            }
            
            res.add(ans);
        }
        return res;
    }
}
2.        
/*
  :    ,    
    :0 ms,     Java       100.00%   
    :39.9 MB,     Java       5.75%   
*/
class Solution {
     
    private int maxdepth = 0;
    public int maxDepth(TreeNode root) {
     
        if (root==null) return 0;
        int depth = Depth(root,1);
        return depth;
    }

    private int Depth(TreeNode root, int i) {
     
        maxdepth = Math.max(i, maxdepth);
        if (root.left!=null) Depth(root.left, i+1);
        if (root.right!=null) Depth(root.right, i+1);
        return maxdepth;
    }
}


/*
    :0 ms,     Java       100.00%   
    :39.7 MB,     Java       5.75%   
*/
class Solution {
     
       public int maxDepth(TreeNode root) {
     
        if (root==null) return 0;
        int depth_left  = maxDepth(root.left);
        int depth_right  = maxDepth(root.right);
        return Math.max(depth_left, depth_right)+1;
    }
}
3.     
/*
    :0 ms,     Java       100.00%   
    :38 MB,     Java       23.75%   
*/
class Solution {
     
    public boolean isSymmetric(TreeNode root) {
     
        if (root==null) return true;
        return judge(root.left,root.right);
    }

    private boolean judge(TreeNode left, TreeNode right) {
     
        if (left==null&&right==null) return true;
        if (left==null||right==null) return false;
        boolean res = (left.val==right.val)&&(judge(left.right, right.left)&&
                judge(left.left, right.right));
        return res;
    }

}    
4.    
/*
    :0 ms,     Java       100.00%   
    :39.6 MB,     Java       6.52%   
*/
class Solution {
     
    private int judge = 0;
    public boolean hasPathSum(TreeNode root, int sum) {
     
        if(root==null) return false;
        return DFS(root,sum,root.val)==1?true:false;
    }

    private int DFS(TreeNode root, int sum, int temp) {
     
        
        if (root.left==null&&root.right==null&&temp==sum) judge = 1;
        if (root.left!=null) DFS(root.left, sum, temp+root.left.val);
        if (root.right!=null) DFS(root.right, sum, temp+root.right.val);
        return judge;
    }
}
5.               
/*
             ,        ;          。
    :2 ms,     Java       97.62%   
    :40.4 MB,     Java       61.90%   
*/
class Solution {
     
    //hashmap           
    HashMap<Integer,Integer> inorderMap = new HashMap<>();
    int[] post;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
     
        post = postorder;
        for (int i =0 ;i<inorder.length;i++){
     
            inorderMap.put(inorder[i],i);
        }
        TreeNode root = create(0,inorder.length-1,0,postorder.length-1);
        return root;
    }

    private TreeNode create(int inorderbegin, int inorderend, int postorderbegin, int postorderend) {
     
        if (inorderbegin>inorderend||postorderbegin>postorderend) return null;
        //        
        int rootNum = inorderMap.get(post[postorderend]);
        TreeNode node = new TreeNode(post[postorderend]);
        //rootNum-1:         
        //rootNum-1-inorderbegin:       
        node.left = create(inorderbegin,rootNum-1,postorderbegin,postorderbegin+rootNum-1-inorderbegin);
        node.right = create(rootNum+1, inorderend, postorderbegin+rootNum-inorderbegin, postorderend-1);
        return node;
    }
}
6.               &&     
/*
             ,        ;         。
    :2 ms,     Java       98.27%   
    :40.3 MB,     Java       63.33%   
*/
class Solution {
     
    //hashmap           
    HashMap<Integer,Integer> inorderMap = new HashMap<>();
    int[] pre;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
     
        pre = preorder;
        for (int i = 0;i<inorder.length;i++){
     
            inorderMap.put(inorder[i],i);
        }
        TreeNode root = create(0,inorder.length-1,0, preorder.length-1);
        return root;
    }
    private TreeNode create(int inorderbegin, int inorderend, int preorderbegin, int preorderend) {
     
        if (inorderbegin>inorderend||preorderbegin>preorderend) return null;
        //        
        int rootNum = inorderMap.get(pre[preorderbegin]);
        TreeNode node = new TreeNode(pre[preorderbegin]);
        node.left = create(inorderbegin,rootNum-1,preorderbegin+1,preorderbegin+rootNum-inorderbegin);
        node.right = create(rootNum+1, inorderend, preorderbegin+rootNum-inorderbegin+1, preorderend);
        return node;
    }
}
7.               
/*
    :2 ms,     Java       42.78%   
    :39.2 MB,     Java       100.00%   
*/
class Solution {
     
    HashMap<Integer,Integer> postorderMap = new HashMap<>();
    int[] preorder ;
    public TreeNode constructFromPrePost(int[] pre, int[] post) {
     
        preorder = pre;
        for(int i = 0;i<pre.length;i++){
     
            postorderMap.put(post[i],i);
        }
        TreeNode root = create(0,pre.length-1,0,post.length-1);
        return root;
    }

    private TreeNode create(int prebegin, int preend, int postbegin, int postend) {
     
        if(prebegin == preend) return new TreeNode(preorder[prebegin]);
        if (prebegin>preend||postbegin>postend)return null;
        TreeNode node = new TreeNode(preorder[prebegin]);
        //                 
        int temp = postorderMap.get(preorder[prebegin+1]);
        //           
        int leftnum = temp-postbegin+1;
        node.left =create(prebegin+1,prebegin+leftnum,
                          postbegin,temp);
        node.right = create(prebegin+leftnum+1, preend, temp+1, postend-1);
        return node;
    }
}
8.                
/*
  BFS      
    :3 ms,     Java       44.59%   
    :40.2 MB,     Java       6.67%   
*/
 public Node connect(Node root) {
     
        if (root==null) return null;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
     
            int size = queue.size();
            for (int i  = 0;i<size;i++){
     
                Node temp = queue.poll();
                //         
                if (i<size-1) temp.next = queue.peek();
                if (temp.left!=null) queue.add(temp.left);
                if (temp.right!=null) queue.add(temp.right);
            }

        }
        return root;
    }
}
9.                 II
/*
  BFS      
    :2 ms,     Java       46.29%   
    :39.4 MB,     Java       33.33%   
*/
 public Node connect(Node root) {
     
        if (root==null) return null;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
     
            int size = queue.size();
            for (int i  = 0;i<size;i++){
     
                Node temp = queue.poll();
                 //         
                if (i<size-1) temp.next = queue.peek();
                if (temp.left!=null) queue.add(temp.left);
                if (temp.right!=null) queue.add(temp.right);
            }

        }
        return root;
    }
}
10.          
/*
  :    (  )
    :7 ms,     Java       99.91%   
    :41.9 MB,     Java       5.71%   
*/
class Solution {
     
 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
     
        if (root==null||root==p||root==q) return root;
        TreeNode judgeleft = lowestCommonAncestor(root.left, p, q);
        TreeNode judgeright = lowestCommonAncestor(root.right, p, q);
     	//          ,  root
        if (judgeleft!=null&&judgeright!=null) return root;
        //       ,     
     	else if (judgeleft==null) return judgeright;
     	//       ,     
        else if (judgeright==null) return judgeleft;
        return null;
    }
}
11.             
/*
   labuladong     ,  StringBuilder        
    :8 ms,     Java       84.31%   
    :41 MB,     Java       100.00%   
*/
class Solution {
     

    public class Codec {
     
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
     
            StringBuilder string = new StringBuilder();
            serialize(string, root);
            return string.toString();
        }

        private void serialize(StringBuilder string, TreeNode root) {
     
            if (root == null) {
     
                string.append("#").append(",");
                return;
            }
            string.append(root.val).append(",");
            serialize(string, root.left);
            serialize(string, root.right);
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
     
            LinkedList<String> nodes = new LinkedList<>();
            for (String temp : data.split(",")) {
     
                nodes.add(temp);
            }
            return deserialize(nodes);
        }

        private TreeNode deserialize(LinkedList<String> nodes) {
     
            if (nodes.isEmpty()) return null;
            String node = nodes.removeFirst();
            if (node.equals("#")) return null;
            TreeNode root = new TreeNode(Integer.parseInt(node));
            root.left = deserialize(nodes);
            root.right = deserialize(nodes);
            return root;
        }

    }

}