655-Print Binary Tree
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", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Note: The height of binary tree is in the range of [1, 10].
問題描述
以m行, n列的二維字串陣列列印出二叉樹, 遵守如下規則
- 行數m需要等於二叉樹的高度
- 列數n需要為奇數
- 根節點的值(字串形式)需要放在第一行的正中間。根節點處於的行和列會將剩餘空間分成兩部分, 左下和右下。你需要將左子樹列印在左下部分, 將右子樹列印在右下部分。左下部分和右下部分需要為同樣大小。即使一個子樹為空, 而另一個子樹非空, 你需要為空子樹的那一邊與非空子樹那邊佔用的同樣大小的空間。然而, 若兩個子樹都為空, 你不需要為它們留空間。
- 每一個未使用的空間為空串”“
- 以同樣的規則列印子樹
問題分析
先通過後序遍歷求出樹的高度height, 也就是m, n =
2^{m} - 1
, 初始化res
然後通過前序遍歷往res裡面填充值, 注意start和end(用來取根節點所在位置), height(用來取所在行數)
解法
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 n = (1 << height) - 1;
List<String> line = new ArrayList();
for(int i = 0;i < n;i++) line.add("");
for(int i = 0;i < height;i++) res.add(new ArrayList(line));
preorder(root, res, 0, n, 0);
return res;
}
public void preorder(TreeNode root, List<List<String>> res, int start, int end, int depth){
if(root == null) return;
int mid = start + (end - start) / 2;
res.get(depth).set(mid, String.valueOf(root.val));
preorder(root.left, res, start, mid, depth + 1);
preorder(root.right, res, mid + 1, end, depth + 1);
}
public int getHeight(TreeNode root){
if(root == null) return 0;
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
}
相關文章
- Traversals of binary tree
- Leetcode Binary Tree PathsLeetCode
- [LintCode] Binary Tree Level Order
- 545. Boundary of Binary Tree
- 257. Binary Tree Paths
- Construct String from Binary TreeStruct
- [LintCode] Check Full Binary Tree
- 257-Binary Tree Paths
- 543-Diameter of Binary Tree
- 563-Binary Tree Tilt
- 654-Maximum Binary Tree
- 814-Binary Tree Pruning
- 110-Balanced Binary Tree
- 226-Invert Binary Tree
- 104. Maximum Depth of Binary Tree
- LeetCode 543. Diameter of Binary TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- 662. Maximum Width of Binary Tree
- 173. Binary Search Tree Iterator
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- 111-Minimum Depth of Binary Tree
- 662-Maximum Width of Binary Tree
- Binary-tree-level-order-traversal
- 104-Maximum Depth of Binary Tree
- 637-Average of Levels in Binary Tree
- 669-Trim a Binary Search Tree
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode