Tuesday, February 12, 2019

655. Print Binary Tree

655Print 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].
-------------------
先计算出高和宽,然后直接递归
ref: https://zxi.mytechroad.com/blog/tree/leetcode-655-print-binary-tree/

/**
 * 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) {
        int h = getHeight(root);
        int w = (1 << h) - 1;
        
        List<List<String>> rt = new ArrayList<>();
        for (int i = 0; i < h; i++) {
            List<String> l = new ArrayList<>();
            
            for (int j = 0; j < w; j++) {
                l.add("");
            }
            
            rt.add(l);
        }
        
        rec(root, rt, 0, w - 1, 0);
        
        return rt;
    }
    
    private void rec(TreeNode root, List<List<String>> rt, int left, int right, int level) {
        if (root == null) return;
        int mid = (left + right) / 2;
        rt.get(level).set(mid, Integer.toString(root.val));
        
        rec(root.left, rt, left, mid - 1, level + 1);
        rec(root.right, rt, mid + 1, right, level + 1);
    }
    
    private int getHeight(TreeNode root) {
        if (root == null) return 0;
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }
}

Solution #2, level order traversal
2个queue,一个记录node,另一个记录node所对应的范围(left, right)
ref: https://leetcode.com/problems/print-binary-tree/discuss/106269/Java-Iterative-Level-Order-Traversal-with-Queue

No comments:

Post a Comment