---
# System prepended metadata

title: 20191121 Tree

---

# 20191121 Tree


```
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Example: 

You may serialize the following tree:

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]"
```

```
public class Codec {
    public static final String NULL_STRING = "null";
    public static final String COMMA = ",";

    // Encodes a tree to a single string.
   public String serialize(TreeNode root) {
        if (root == null) return "";
        Queue<TreeNode> q = new LinkedList<>();
        StringBuilder res = new StringBuilder();
        q.add(root);
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            if (node == null) {
                res.append(NULL_STRING + COMMA);
                continue;
            }
            res.append(node.val + COMMA);
            q.add(node.left);
            q.add(node.right);
        }
        return res.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null || data == "") return null;
        data = data.replace("[", "");
        data = data.replace("]", "");
        Queue<TreeNode> queue = new LinkedList<>();
        String[] nodeStrArr = data.split(COMMA);
        TreeNode root = new TreeNode(Integer.valueOf(nodeStrArr[0]));
        queue.offer(root);
        for (int i = 1; i < nodeStrArr.length; i++) {
            TreeNode node = queue.poll();
            if (!nodeStrArr[i].equals(NULL_STRING)) {
                TreeNode left = new TreeNode(Integer.valueOf(nodeStrArr[i]));
                node.left = left;
                queue.offer(left);
            }
            if (!nodeStrArr[i + 1].equals(NULL_STRING)) {
                TreeNode right = new TreeNode(Integer.valueOf(nodeStrArr[i + 1]));
                node.right = right;
                queue.offer(right);
            }
            i++;
        }
        
        return root;
    }
}
```
```
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3
 

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3
 

Note:
Bonus points if you could solve it both recursively and iteratively.
```
```
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return false;
        return helper(root.left, root.right);
    }
    
//      2      2
//     / \    / \
//    3  4   4  3
    public boolean helper(TreeNode left, TreeNode right) {
         if (left == null || right == null) {
            return left == right;
         } else if (left.val != right.val) {
            return false;
         }
        return helper(left.left, right.right) && helper(left.right, right.left);
    }
}
```

```
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root.left);
        stack.push(root.right);
        while (!stack.empty()) {
            TreeNode n1 = stack.pop(), n2 = stack.pop();
            if (n1 == null && n2 == null) {
                continue;
            }
            if (n1 == null || n2 == null || n1.val != n2.val) {
                return false;
            }
            stack.push(n1.left);
            stack.push(n2.right);
            stack.push(n1.right);
            stack.push(n2.left);
        }
        return true;
    }
}
```



[3,2,3,1,2,4,5,5,6], k = 3
7, 6, 5

min Heap
         1
       /   \
      2    2
     / \   /\
    3   3  4 5
    /\
    5 6