Print Binary Tree问题及解法
来源:互联网 发布:php 单例模式 编辑:程序博客网 时间:2024/05/18 17:01
问题描述:
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.
示例:
Input: 1 / 2Output:[["", "1", ""], ["2", "", ""]]
Input: 1 / \ 2 3 \ 4Output:[["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]]
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矩阵,再次遍历树,将节点值赋予到数组中去。
过程详见代码:
/** * 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 maxDepth = 0;bl(root, 1, maxDepth);vector<vector<string>> res(maxDepth, vector<string>((int)pow(2, maxDepth) - 1, ""));if (res.size() > 0)bl2(res, root, 0, res[0].size() - 1, 1);return res;}void bl(TreeNode* root, int depth, int& maxDepth){if (root == nullptr) return;maxDepth = max(depth,maxDepth);bl( root->left, depth + 1, maxDepth);bl( root->right, depth + 1, maxDepth);}void bl2(vector<vector<string>>& res,TreeNode* root,int i ,int j,int depth){if (root == nullptr) return;res[depth - 1][(i + j) / 2] = to_string(root->val);bl2(res, root->left, i, (i + j) / 2 - 1, depth + 1);bl2(res, root->right, (i + j) / 2 + 1, j, depth + 1);}};
阅读全文
0 0
- Print Binary Tree问题及解法
- Balanced Binary Tree问题及解法
- Diameter of Binary Tree问题及解法
- Binary Tree Tilt问题及解法
- Binary Tree Inorder Traversal问题及解法
- Validate Binary Search Tree问题及解法
- Binary Tree Preorder Traversal问题及解法
- Binary Search Tree Iterator问题及解法
- Maximum Binary Tree问题及解法
- Maximum Depth of Binary Tree问题及解法
- Binary Tree Level Order Traversal II问题及解法
- Convert Sorted Array to Binary Search Tree问题及解法
- Minimum Depth of Binary Tree问题及解法
- Find Mode in Binary Search Tree问题及解法
- Average of Levels in Binary Tree问题及解法
- Construct String from Binary Tree问题及解法
- Binary Tree Level Order Traversal问题及解法
- Binary Tree Zigzag Level Order Traversal问题及解法
- 在JAVA中,== 和equals使用记录
- Android错误之(Android 6.0)Unable to add window android.view.ViewRootImpl$W@d4521e8 -- permission denied
- sql 语句 记录
- 基数排序
- 移动web开发三之头部和底部
- Print Binary Tree问题及解法
- 使用ShellExecute函数打开IE浏览器下的新窗口
- android开发--ProgressBar 颜色/样式的设置
- 3347-数据结构实验之数组三:快速转置
- 进程间通信之messenger
- freemarker基本命令学习
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- ios百度地图更改罗盘指南针位置
- 测试