655 Print Binary Tree

1. Question

Print a binary tree in an m*n 2D string array following these rules:

  1. The row numbermshould be equal to the height of the given binary tree.

  2. The column numbernshould always be an odd number.

  3. The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them.

  4. Each unused space should contain an empty string"".

  5. Print the subtrees following the same rules.

Example 1:

Input:

     1
    /
   2

Output:

[["", "1", ""],
 ["2", "", ""]]

Example 2:

Input:

     1
    / \
   2   3
    \
     4

Output:

[["", "", "", "1", "", "", ""],
 ["", "2", "", "", "", "3", ""],
 ["", "", "4", "", "", "", ""]]

Example 3:

Input:

      1
     / \
    2   5
   / 
  3 
 / 
4 

Output:

[["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""]
 ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""]
 ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]
 ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]

Note:The height of binary tree is in the range of [1, 10].

2. Implementation

(1) DFS

class Solution {
    public List<List<String>> printTree(TreeNode root) {
        List<List<String>> res = new ArrayList<>();

        if (root == null) {
            return res;
        }

        int rows = getHeight(root);
        int cols = (1 << rows) - 1;
        String[][] table = new String[rows][cols];

        for (String[] row : table) {
            Arrays.fill(row, "");
        }

        fillTable(root, 0, 0, cols - 1, table);

        for (String[] row : table) {
            res.add(Arrays.asList(row));
        }
        return res;
    }

    public void fillTable(TreeNode node, int row, int leftCol, int rightCol, String[][] table) {
        if (node == null) {
            return;
        }

        int mid = leftCol + (rightCol - leftCol) / 2;
        table[row][mid] = node.val + "";

        fillTable(node.left, row + 1, leftCol, mid - 1, table);
        fillTable(node.right, row + 1, mid + 1, rightCol, table);
    }

    public int getHeight(TreeNode node) {
        if (node == null) {
            return 0;
        }

        return 1 + Math.max(getHeight(node.left), getHeight(node.right));
    }
}

(1) BFS

class Solution {
     class Cell {
        TreeNode node;
        int row;
        int left;
        int right;

        public Cell(TreeNode node, int row, int left, int right) {
            this.node = node;
            this.row = row;
            this.left = left;
            this.right = right;
        }
    }

    public List<List<String>> printTree(TreeNode root) {
        List<List<String>> res = new ArrayList<>();

        if (root == null) {
            return res;
        }

        int rows = getHeight(root);
        int cols = (1 << rows) - 1;

        String[][] table = new String[rows][cols];
        for (String[] row : table) {
            Arrays.fill(row, "");
        }

        Queue<Cell> queue = new LinkedList<>();
        queue.add(new Cell(root, 0, 0, cols - 1));

        while (!queue.isEmpty()) {
            Cell curCell = queue.remove();
            int mid = curCell.left + (curCell.right - curCell.left) / 2;
            table[curCell.row][mid] = curCell.node.val + "";

            if (curCell.node.left != null) {
                queue.add(new Cell(curCell.node.left, curCell.row + 1, curCell.left, mid - 1));
            }

            if (curCell.node.right != null) {
                queue.add(new Cell(curCell.node.right, curCell.row + 1, mid + 1, curCell.right));
            }
        }

        for (String[] row : table) {
            res.add(Arrays.asList(row));
        }
        return res;
    }

    public int getHeight(TreeNode node) {
        if (node == null) {
            return 0;
        }

        return 1 + Math.max(getHeight(node.left), getHeight(node.right));
    }
}

3. Time & Space Complexity

DFS: 时间复杂度O(h * 2^h), 空间复杂度O(h * 2^h)

BFS: 时间复杂度O(h * 2^h), 空间复杂度O(h * 2^h)

Last updated