655. Print Binary Tree

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:

  • The height of the tree is height and the number of rows m should be equal to height + 1.
  • The number of columns n should be equal to 2height+1 - 1.
  • Place the root node in the middle of the top row (more formally, at location res[0][(n-1)/2]).
  • For each node that has been placed in the matrix at position res[r][c], place its left child at res[r+1][c-2height-r-1] and its right child at res[r+1][c+2height-r-1].
  • Continue this process until all the nodes in the tree have been placed.
  • Any empty cells should contain the empty string "".

Return the constructed matrix res.

Example 1:

Input: root = [1,2]
Output: 
[["","1",""],
 ["2","",""]]

Example 2:

Input: root = [1,2,3,null,4]
Output: 
[["","","","1","","",""],
 ["","2","","","","3",""],
 ["","","4","","","",""]]

Constraints:

  • The number of nodes in the tree is in the range [1, 210].
  • -99 <= Node.val <= 99
  • The depth of the tree will be in the range [1, 10].

题目:给定一棵二叉树,生成一个字符串数组

思路:先判断二叉树的深度,以此生成一个空的字符串数组。数组行数为二叉树的层数,列数为pow(2, height) -1。用队列来记录一个满二叉树,空着的地方用NULL表示。放到相应的位置。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getDepth(TreeNode* node, int level){
        if(!node) return level;
        int l = getDepth(node->left, level+1);
        int r = getDepth(node->right, level+1);
        return max(l, r);
    }
    vector<vector<string>> printTree(TreeNode* root) {
        int Depth = getDepth(root, -1), col = pow(2, Depth+1)-1;
        queue<TreeNode*> q;
        q.push(root);
        vector<vector<string>> res(Depth+1, vector<string>(col, ""));
        int level = 0;
        while(!q.empty() && level <= Depth){
            int dis = col / 2, pos = dis, size = (int)pow(2, level);
            for(int i = 0; i < size; i++){
                TreeNode* node = q.front();
                q.pop();
                if(node){
                    res[level][pos] = to_string(node->val);
                    q.push(node->left);
                    q.push(node->right);
                } else {
                    q.push(NULL);
                    q.push(NULL);
                }
                pos += dis * 2 + 2;
            }
            level ++;
            col = dis;
        }
        return res;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值