655. Print Binary Tree Medium
1/**
2 * [655] Print Binary Tree
3 *
4 * Given the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree. The formatted layout matrix should be constructed using the following rules:
5 *
6 * The height of the tree is height and the number of rows m should be equal to height + 1.
7 * The number of columns n should be equal to 2^height+1 - 1.
8 * Place the root node in the middle of the top row (more formally, at location res[0][(n-1)/2]).
9 * For each node that has been placed in the matrix at position res[r][c], place its left child at res[r+1][c-2^height-r-1] and its right child at res[r+1][c+2^height-r-1].
10 * Continue this process until all the nodes in the tree have been placed.
11 * Any empty cells should contain the empty string "".
12 *
13 * Return the constructed matrix res.
14 *
15 * Example 1:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/print1-tree.jpg" style="width: 141px; height: 181px;" />
17 * Input: root = [1,2]
18 * Output:
19 * [["","1",""],
20 * ["2","",""]]
21 *
22 * Example 2:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/print2-tree.jpg" style="width: 207px; height: 302px;" />
24 * Input: root = [1,2,3,null,4]
25 * Output:
26 * [["","","","1","","",""],
27 * ["","2","","","","3",""],
28 * ["","","4","","","",""]]
29 *
30 *
31 * Constraints:
32 *
33 * The number of nodes in the tree is in the range [1, 2^10].
34 * -99 <= Node.val <= 99
35 * The depth of the tree will be in the range [1, 10].
36 *
37 */
38pub struct Solution {}
39use crate::util::tree::{TreeNode, to_tree};
40
41// problem: https://leetcode.com/problems/print-binary-tree/
42// discuss: https://leetcode.com/problems/print-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46// Definition for a binary tree node.
47// #[derive(Debug, PartialEq, Eq)]
48// pub struct TreeNode {
49// pub val: i32,
50// pub left: Option<Rc<RefCell<TreeNode>>>,
51// pub right: Option<Rc<RefCell<TreeNode>>>,
52// }
53//
54// impl TreeNode {
55// #[inline]
56// pub fn new(val: i32) -> Self {
57// TreeNode {
58// val,
59// left: None,
60// right: None
61// }
62// }
63// }
64use std::rc::Rc;
65use std::cell::RefCell;
66impl Solution {
67 pub fn print_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<String>> {
68 vec![]
69 }
70}
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn test_655() {
80 }
81}
82
Back
© 2025 bowen.ge All Rights Reserved.