1. 程式人生 > >LeetCode_655. Print Binary Tree

LeetCode_655. Print Binary Tree

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

  1. The row number m should be equal to the height of the given binary tree.
  2. The column number n should 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].

思路:先求出樹的深度,在求出樹的寬度,將輸出看成二維陣列將所有的值初始化為“”,在根據他們的在行數判斷位置

class Solution {
    public List<List<String>> printTree(TreeNode root) {
        List<List<String>> res = new LinkedList<>();   /*儲存結果*/
        /*獲取樹的高度*/
        int height = root == null ? 1: getHeight(root);
        /*獲取樹的深度*/
        int rows = height,columns = (int)(Math.pow(2,height) - 1);
        /*Row 來儲存每一行的數值*/
        List<String> row  = new ArrayList<>();
        /*先將所有行的值都設定為""*/
        for(int i = 0;i< columns;i++)row.add("");
        /*將每行新增到結果中*/
        for(int i = 0;i<rows;i++)res.add(new ArrayList<>(row));
        
        populateRes(root,res,0,rows,0,columns -1);
        return res;
    }
    public void populateRes(TreeNode root,List<List<String>> res,int row,int totalRows,int i,int j){
        if(row == totalRows || root == null)return ;
        /*插入當前的節點的值*/
        res.get(row).set((i+j)/2,Integer.toString(root.val));
        /*插入當前節點的左節點*/
        populateRes(root.left, res, row+1, totalRows, i, (i+j)/2 - 1);
        /*插入當前節點的右節點*/
        populateRes(root.right, res, row+1, totalRows, (i+j)/2+1, j);
    }
    /*獲取樹的高度*/
    public int getHeight(TreeNode root){
        if(root == null)return 0;
        return 1+Math.max(getHeight(root.left),getHeight(root.right));
    }
}