zoukankan      html  css  js  c++  java
  • LeetCode 655. Print Binary Tree (C++)

    题目:

    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].

    分析:

    给定一颗二叉树,按照格式打印这个树。

    这道题的输出是一个二维数组,我们可以观察到,数组的高度等于树的高度h,宽度等于2^h-1,所以针对这道题,我们要先求出给定树的高度,再开辟相应大小的数组。

    其次我们再观察,根节点处于二维数组第一行的中间,将左右两边均等的分开,其左右子树的根节点分别位于分开的两行的中间,可以想到使用递归来实现。

    每次填充时传递一个高度参数用来锁定二维数组的行号。

    程序:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<string>> printTree(TreeNode* root) {
            int h = getTreeH(root);
            //w = 2^h-1
            int w = (1<<h)-1;
            vector<vector<string>> res(h, vector<string>(w));
            printTree(root, res, 0, 0, w-1);
            return res;
        }
        void printTree(TreeNode* root, vector<vector<string>>& res, int h, int l, int r){
            if(!root) return;
            int mid = (l+r)/2;
            res[h][mid] = to_string(root->val);
            printTree(root->left, res, h+1, l, mid-1);
            printTree(root->right, res, h+1, mid+1, r);     
        }
        //get tree height
        int getTreeH(TreeNode* root){
            if(!root) return 0;
            return max(getTreeH(root->left), getTreeH(root->right))+1;
        }
    };
  • 相关阅读:
    NetBeans 时事通讯(刊号 # 52 Apr 15, 2009)
    Linux 3.8.1 电源管理之OMAP Clock Domain分析
    基础架构部_超大规模数据平台架构师(上海)
    C Programming/Pointers and arrays Wikibooks, open books for an open world
    这个帖子介绍了关于structure和及struct arrary 作为参数 传递
    thinking point
    velocity
    枫芸志 » 【C】int与size_t的区别
    Pointers and Text Strings
    comp.lang.c Frequently Asked Questions 非常 好
  • 原文地址:https://www.cnblogs.com/silentteller/p/10721048.html
Copyright © 2011-2022 走看看