二叉树

7 序列和反序列话

class Solution {
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    public String serialize(TreeNode root) {
        // write your code here
        if (root == null){
            return "{}";
        }
        ArrayList<TreeNode> queue = new ArrayList<>();
        queue.add(root);
        for (int i = 0; i < queue.size(); i++){
            TreeNode node = queue.get(i);
            if (node == null) continue;
            queue.add(root.left);
            queue.add(root.right);
        }
        while (queue.get(queue.size() - 1) == null){
            queue.remove(queue.get(queue.size() - 1));
        }
        StringBuilder sb = new StringBulder();
        sb.append("{");
        sb.append(queue.get(0));
        for (int i = 1; i < queue.size(); i++){
            if (queue.get(i) != null){
                sb.append(",");
                sb.append(queue.get(i));
            } else{
                sb.append(",#");
            }
        }
        sb.append("}");
        return sb.toString();
    }
    
    
    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    public TreeNode deserialize(String data) {
        // write your code here
        if ("{}".equals(data)){
            return null;
        }
        String[] vals = data.substring(1, data.length() - 1).split(",");
        ArrayList<TreeNode> queue = new ArrayList<>();
        TreeNode root = new TreeNode(vals[0]);
        queue.add(root);
        boolean isL = true;
        int  index = 0;
        for (int i = 1; i < vals.length; i++){
            if (!"#".equeal(vals[i])){
                TreeNode node = new TreeNode(Integer.praiseInt(vals[i]));
                if (isL){
                    queue.get(index).left = node;
                } else{
                    queue.get(index).right = node;
                }
                queue.add(node);
            }
            if (!isL){
                index++;
            }
            isL = !isL;
        }
        return root;
    }
}
View Code

 375 克隆二叉树

    public TreeNode cloneTree(TreeNode root) {
        // Write your code here
        if (root == null) return null;
        TreeNode newR = new TreeNode(root.val);
        newR.left = cloneTree(root.left);
        newR.right = cloneTree(root.right);
        return newR;
    }
View Code

245 子树

    public boolean isSubtree(TreeNode T1, TreeNode T2) {
        // write your code here
        if (T2 == null) return true;
        if (T1 == null) return false;
        if (Eq(T1, T2)) return true;
        if (isSubtree(T1.left, T2) || isSubtree(T1.right, T2)) return true;
        return false;
    }
    public boolean Eq(TreeNode t1, TreeNode t2){
        if (t1 == null || t2 == null){
            return t1 == t2;
        }
        if (t1.val != t2.val){
            return false;
        }
        return Eq(t1.left, t2.left) && Eq(t1.right, t2.right);
    }
View Code

453 展平二叉树

    public void flatten(TreeNode root) 
    {
        // write your code here
        LinkedList<TreeNode> pre = new LinkedList<TreeNode>();
        pre.add(null);
        helper(root, pre);
    }
    public void helper(TreeNode root, LinkedList<TreeNode> pre)
    {
        if (root == null) return;
        TreeNode right = root.right;
        if (pre != null){
            pre.left = null;
            pre.right = root;
        }
        pre.set(0, root);
        helper(root.left, pre);
        helper(right, pre);
    }
View Code

376 二叉树路径和

    public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
        // Write your code here
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Stack<Integer> stack = new Stack<>();
        findPath(root, stack, 0, target, res);
        return res;
    }
    public void findPath(TreeNode root, Stack<Integer> stack, int sum,
     int target, List<List<Integer>> res){
        sum += root.val;
        stack.push(root.val);
        if (sum == target && root.left == null && root.right == null){
            List<Integer> list = new ArrayList<>(stack);
            res.add(list);
            stack.pop();
            return;
        } else{
            if (root.left != null){
                findPath(root.left, stack,  sum,target, res);}
            if (root.right != null){
                findPath(root.right, stack, sum, target, res);
            }
            stack.pop();
        }
    }
View Code

 二叉树路径

    public List<String> binaryTreePaths(TreeNode root) {
        // Write your code here
        ArrayList<String> paths = new ArrayList<>();
        if (root == null) return paths;
        List<String> left = binaryTreePaths(root.left);
        List<String> right = binaryTreePaths(root.right);
        for (String path : left){
            paths.add(root.val + "->" +  path);
        }
         for (String path : right){
            paths.add(root.val + "->" +  path);
        }
        if (paths.size() == 0){
            paths.add("" + root.val);
        }
        return paths;
    }
View Code

 

posted on 2017-06-15 14:48  wheleetcode  阅读(105)  评论(0编辑  收藏  举报