655. Print Binary Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigFatSheep/article/details/83870981

Description

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

The row number m should be equal to the height of the given binary tree.
The column number n should always be an odd number.
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.
Each unused space should contain an empty string “”.
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”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]]

Problem URL


Solution

将一棵二叉树打印到List表示的二维矩阵中。

So the most tricky thing of this problem is the format of printed list matrix. I found in the disscussion that every row have 2^n - 1 colums. n is the height of the tree. So we could get the height of the tree first. Then construct an empty list with “”. Then recursively add node.val into it. The index of each node should be exactly at the middle of its span. So it is (0 + width - 1) / 2 for root node.

Code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<String>> printTree(TreeNode root) {
        List<List<String>> res = new ArrayList<>();
        if (root == null){
            return res;
        }
        int height = getHeight(root);
        int width = (int)(Math.pow(2, height) - 1);
        List<String> row = new ArrayList<>();
        for (int i = 0; i < width; i++){
            row.add("");
        }
        for (int i = 0; i < height; i++){
            res.add(new ArrayList<String>(row));
        }
        printHelper(root, res, 0, height, 0, width - 1);
        return res;
    }
    
    private void printHelper(TreeNode node, List<List<String>> res, int row, int height, int i, int j){
        if (node == null || row == height){
            return;
        }
        res.get(row).set((i + j) / 2, Integer.toString(node.val));
        printHelper(node.left, res, row + 1, height, i, (i + j) / 2 - 1);
        printHelper(node.right, res, row + 1, height, (i + j) / 2 + 1, j);
    }
    
    private int getHeight(TreeNode node){
        if (node == null){
            return 0;
        }
        return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
    }
}

Time Complexity: O(n)
Space Complexity: O(n)


Review

猜你喜欢

转载自blog.csdn.net/BigFatSheep/article/details/83870981