作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/print-binary-tree/description/

题目描述

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

题目大意

使用二维数组打印出二叉树。如果某个位置是空节点,也应该给它留出对应的位置。

解题方法

DFS

最初认为,给空节点留下位置加大了题目难度。其实真正理解题目要考察的内容之后,发现这个条件让我们可以使用完全二叉树的数学公式,所以使题目变得简单了。

这个题首先要求出树的高度,然后求出完全二叉树的宽度。根据高度和宽度构建出二维数组,再利用递归求出每个层次的每个节点对应的二维数组的位置,设为节点的值即可。

如果是DFS去做的话,每次向下搜索的时候,需要传入这个子区间的起始位置。根节点放在中间。

说的容易,难的再求这些公式,真的就是完全二叉树的一些知识。具体的就看这两篇文章吧,很详细。

  1. https://leetcode.com/problems/print-binary-tree/discuss/106273/Simple-Python-with-thorough-explanation

  2. https://leetcode.com/problems/print-binary-tree/discuss/106250/Python-Straight-Forward-Solution

代码:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def printTree(self, root):
"""
:type root: TreeNode
:rtype: List[List[str]]
"""
if not root: return [""]
def getDepth(root):
if not root:
return 0
return 1 + max(getDepth(root.left), getDepth(root.right))
d = getDepth(root)
cols = 2 ** d - 1
self.res = [["" for i in range(cols)] for j in range(d)]
def helper(root, d, pos):
self.res[-d - 1][pos] = str(root.val)
if root.left: helper(root.left, d - 1, pos - 2 ** (d - 1))
if root.right: helper(root.right, d - 1, pos + 2 ** (d - 1))
helper(root, d - 1, 2 ** (d - 1) - 1)
return self.res

上面的python代码不够直观,如果使用类似二分查找的方式,使用left和right表示左右区间,那么代码可以写成下面这样。

/**
* 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) {
const int h = depth(root);
int w = pow(2, h) - 1;
vector<vector<string>> res(h, vector<string>(w, ""));
dfs(root, res, 0, 0, w);
return res;
}
private:
int depth(TreeNode* root) {
if (!root) return 0;
return max(depth(root->left), depth(root->right)) + 1;
}
// [left, right)
void dfs(TreeNode* root, vector<vector<string>>& res, int depth, int left, int right) {
if (!root || depth == res.size()) return;
int mid = left + (right - left) / 2;
res[depth][mid] = to_string(root->val);
dfs(root->left, res, depth + 1, left, mid);
dfs(root->right, res, depth + 1, mid + 1, right);
}
};

BFS

使用BFS的话,就是类似于层次遍历的解法。这个题困难的地方在于不好找出节点要放置的位置。所以使用了两个BFS,一个BFS用来遍历节点,另一个BFS用来保存每个节点对应的起始位置。每次把根节点放到中间的地方,也就有点类似于二分查找。

C++代码如下:

/**
* 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) {
const int h = depth(root);
int w = pow(2, h) - 1;
int curH = -1;
vector<vector<string>> res(h, vector<string>(w, ""));
queue<TreeNode*> q;
q.push(root);
// [first, second)
queue<pair<int, int>> idxQ;
idxQ.push({0, w});
while (!q.empty()) {
curH++;
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* node = q.front(); q.pop();
auto idx = idxQ.front(); idxQ.pop();
if (!node) continue;
int left = idx.first, right = idx.second;
int mid = left + (right - left) / 2;
res[curH][mid] = to_string(node->val);
q.push(node->left);
q.push(node->right);
idxQ.push({left, mid});
idxQ.push({mid + 1, right});
}
}
return res;
}
private:
int depth(TreeNode* root) {
if (!root) return 0;
return max(depth(root->left), depth(root->right)) + 1;
}
};

上面需要同时维护两个队列,其实可以使用一个队列,这个队列同时维护了该节点以及该节点所在的区间的左右位置。而在BFS中是不用维护当前的高度的,BFS可以存储当前属于哪一层。

C++代码如下:

/**
* 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 d = depth(root);
int w = (1 << d) - 1;
queue<pair<TreeNode*, pair<int, int>>> q; // TreeNode*, start, end
q.push({root, {0, w}});
vector<vector<string>> res(d, vector<string>(w));
int curd = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
auto f = q.front(); q.pop();
TreeNode* node = f.first;
int start = f.second.first;
int end = f.second.second;
if (!node) continue;
int mid = start + (end - start) / 2;
res[curd][mid] = to_string(node->val);
q.push({node->left, {start, mid - 1}});
q.push({node->right, {mid + 1, end}});
}
++curd;
}
return res;
}
int depth(TreeNode* root) {
if (!root) return 0;
return max(depth(root->left), depth(root->right)) + 1;
}
};

日期

2018 年 3 月 4 日
2018 年 12 月 18 日 —— 改革开放40周年
2019 年 2 月 25 日 —— 二月就要完了

【LeetCode】655. Print Binary Tree 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. [LeetCode] 655. Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  3. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  4. LeetCode 965 Univalued Binary Tree 解题报告

    题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...

  5. LeetCode 655. Print Binary Tree (C++)

    题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...

  6. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  7. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  9. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

随机推荐

  1. 修复UE4编辑器,ClearLog操作导致的崩溃

    UE4 4.24.3版本,编辑器Output Log窗口中,右键--Clear Log操作很大概率会导致编辑器奔溃:解决办法: 相关文件: Engine\Source\Developer\Output ...

  2. 学习java 7.12

    学习内容: File是文件和目录路径名的抽象表示,File封装的不是一个真正存在的文件,仅仅是一个路径名 File类的方法 绝对目录和相对目录的区别 字节流 使用字节输出流写数据的步骤 : 创建字节输 ...

  3. 100个Shell脚本——【脚本4】自定义rm命令

    [脚本4]自定义rm命令 linux系统的rm命令太危险,一不小心就会删除掉系统文件. 写一个shell脚本来替换系统的rm命令,要求当删除一个文件或者目录时,都要做一个备份,然后再删除.下面分两种情 ...

  4. When do we pass arguments by reference or pointer?

    在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...

  5. notepad++ 连接远程服务器

    前言:为了便于编辑 linux 上的文件,因此通过 notepad++ 连接服务器后打开,编辑完,保存即可 1. 打开 notepad++,安装插件 2. 搜索 NppFtp,找到后 点击 安装/in ...

  6. 智龙开发板搭建llsp环境

    智龙开发板搭建llsp(linux+lighttpd+sqlite3+php)环境 1. 准备 1. 智龙开发板V3 2. 软件编译环境:VirtualBox6+CentOS6.10-i386.min ...

  7. 制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制)

    实现的功能: l  APP控制月球灯 l  本地月球灯控制 l  APP控制"大白"颜色,实现各种颜色变身 l  门状态传感器状态APP显示 l  网络状态指示灯,连接服务器长亮, ...

  8. 剖析虚幻渲染体系(13)- RHI补充篇:现代图形API之奥义与指南

    目录 13.1 本篇概述 13.1.1 本篇内容 13.1.2 概念总览 13.1.3 现代图形API特点 13.2 设备上下文 13.2.1 启动流程 13.2.2 Device 13.2.3 Sw ...

  9. 『学了就忘』Linux服务管理 — 76、RPM包安装的服务管理

    目录 1.独立服务的启动管理 2.独立服务的自启动管理 方式一: 方式二:(推荐) 方式三: 3.验证 1.独立服务的启动管理 (1)使用/etc/init.d/目录中的启动脚本启动服务(推荐) [r ...

  10. Windows下安装xampp的PHP扩展(redis为例)

    (1)PHP的windowns扩展下载网址:https://windows.php.net/downloads/pecl/releases/ (2)Ctrl+f查询你要下载的扩展名 注意:扩展的版本要 ...