LeetCode 655. Print Binary Tree (C++)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了LeetCode 655. Print Binary Tree (C++),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2678字,纯文字阅读大概需要4分钟。
内容图文
题目:
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].
分析:
给定一颗二叉树,按照格式打印这个树。
这道题的输出是一个二维数组,我们可以观察到,数组的高度等于树的高度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-1int 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 heightint getTreeH(TreeNode* root){ if(!root) return0; return max(getTreeH(root->left), getTreeH(root->right))+1; } };
原文:https://www.cnblogs.com/silentteller/p/10721048.html
内容总结
以上是互联网集市为您收集整理的LeetCode 655. Print Binary Tree (C++)全部内容,希望文章能够帮你解决LeetCode 655. Print Binary Tree (C++)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。