655. Print Binary Tree

xiaoxiao2021-02-28  72

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", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]

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

思路:先确定返回数组的大小并初始化好,我是分别求的rows和columns,就是递归收集的过程,

好像rows和columns有个固定关系,用个数组dp一下就求出关系来了

确定了行数和列数,然后就是递归遍历数填充数组,用一个center + range来表示当前树使用了原始数组中的哪些数

import java.util.ArrayList; import java.util.List; public class Solution { public List<List<String>> printTree(TreeNode root) { List<List<String>> ret = new ArrayList<List<String>>(); int[] a = get(root); for(int i=0; i<a[0]; i++) { List<String> t = new ArrayList<String>(); for(int j=0; j<a[1]; j++) t.add(""); ret.add(t); } fill(root, ret, a[1]/2, a[1]/2, 0); return ret; } private void fill(TreeNode root, List<List<String>> ret, int i, int range, int h) { if(root == null) return; ret.get(h).set(i, root.val+""); fill(root.left, ret, i-range/2-1, range/2, h+1); fill(root.right, ret, i+range/2+1, range/2, h+1); } private int[] get(TreeNode root) { if(root == null) return new int[]{0, 0}; int[] l = get(root.left); int[] r = get(root.right); return new int[]{1+Math.max(l[0], r[0]), 1+2*Math.max(l[1], r[1])}; } }

class Solution: def printTree(self, root): """ :type root: TreeNode :rtype: List[List[str]] """ def dfs_get(t): if not t: return (0,0) l, r = dfs_get(t.left), dfs_get(t.right) return (1+max(l[0],r[0]), 1+2*max(l[1],r[1])) rows, cols = dfs_get(root) ret = [['' for _ in range(cols)] for _ in range(rows)] def dfs_fill(t, center, width, h): if not t: return ret[h][center] = str(t.val) dfs_fill(t.left, center-width//2-1, width//2, h+1) dfs_fill(t.right, center+width//2+1, width//2, h+1) dfs_fill(root, cols//2, cols//2, 0) return ret

转载请注明原文地址: https://www.6miu.com/read-46457.html

最新回复(0)