Leetcode 655. Print Binary Tree 打印二叉树 解题报告

如何用capl操控程控电源(RS232) capl脚本操控RS232 阅读详情

给一个二叉树,要求进行打印,也就是在一个高度*宽度的矩阵里面打印就好了,每个子树的root打印在当前子区间的中间就好了

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获得树的高度,第二次打印,注意根据当前层高计算相应的移动位置就可以了

# 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]]
        """
        import math
        def dfs(root, h):
            if root:
                return max(dfs(root.left,h+1), dfs(root.right,h+1))
            else :
                return h
        height = dfs(root, 0)
        width = 2 ** height -1
        # 初始化
        res = [ ["" for j in range(width)] for i in range(height)]
        # dfs print
        def dfs_print(res,root,h,pos):
            if root:
                res[h - 1][pos] = '%d' % root.val
                dfs_print(res, root.left, h+1, pos-2**(height - h - 1))
                dfs_print(res, root.right, h+1, pos+2**(height - h - 1))
        dfs_print(res,root,1,width/2)
        return res
基于Django+Vue3的目标检测系统设计与实现,Web前后端分离,Vue3前台系统+Django后台管理系统开发,YOLOv11 Web目标检测,实现图片、视频和摄像头检测等功能,全网独发 基于Django+Vue3的智能目标检测系统设计与实现,Web前后端分离,YOLOv11 Web目标检测,Vue3前台系统+Django后台管理系统开发,实现图片检测、视频检测、摄像头检测、登录、注册和个人中心功能 阅读详情

相关推荐

YOLOv8/v9 遥感图像目标检测实战指南:建筑物检测与变化监测

遥感图像在城市规划、灾害评估、农业监管等领域应用广泛,但传统目标检测方法存在效率低、精度不足、难以适配遥感图像大尺寸、多尺度目标等问题。本文以“建筑物检测与变化监测”为核心,基于DOTA、NWPU VHR-10等公开数据集,系统讲解YOLOv8/v9在遥感图像中的实战流程。从数据获取、格式转换、模型训练优化,到大图切片推理、建筑物变化监测,全程提供可复现的代码与参数配置。通过针对性优化(旋转增强、切片推理等),模型在NWPU VHR-10数据集上mAP50达0.85以上,可适配卫星影像、无人机航拍等多场景

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 448

图解LeetCode——652. 寻找重复的子树(难度:中等)

那么,在上面的描述中,我们在将子树转化为字符串的时候,指出可以采用前序或后续遍历,为什么不能采用中序遍历呢?请看下面的图示,当我们采用中序遍历的时候,我们发现,针对树A和树B,转换后的结果(不同节点,我们采用“/”分割)是相同的,但是树A和树B却不是重复的子树。根据题意,我们要找出重复的子树,那么,就需要我们针对给出的树进行遍历,来统计这个树是由哪些子树构成的。拼装的树的字符串(每个节点以“/”分割),value存储的是遍历子树过程中,相同子树出现的个数。【输出】[[2,3],[3]]

爪哇缪斯的博客 403

芒果YOLO11改进17:即插即用 | 损失函数Repulsion Loss|Repulsion 解决目标遮挡场景下的目标检测,为解决密集人群检测中遮挡设计的损失函数,打造高效

即插即用 | 损失函数Repulsion Loss|Repulsion 解决目标遮挡场景下的目标检测,为解决密集人群检测中遮挡设计的损失函数,打造高效

包括YOLOv5、YOLOv7、YOLOv8等模型改进 467

LeetCode 652——寻找重复的子树

一、题目介绍 给定一棵二叉树,返回所有重复的子树。对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。 两棵树重复是指它们具有相同的结构以及相同的结点值。 示例 1: 下面是两个重复的子树: 和 4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-duplicate-subtre...

Asher 494

LeetCode 每日一题——652. 寻找重复的子树

LeetCode 每日一题——652. 寻找重复的子树

SK_Jaco的博客 413

LeetCode·每日一题·652.寻找重复的子树·递归·序列化

序列化

m0_64560763的博客 233

Leetcode 652:寻找重复的子树(超详细的解法!!!)

给定一棵二叉树,返回所有重复的子树。对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。 两棵树重复是指它们具有相同的结构以及相同的结点值。 示例 1: 1 / \ 2 3 / / \ 4 2 4 / 4 下面是两个重复的子树: 2 / 4 和 4...

coordinate的博客 1803

LeetCode //C - 655. Print Binary Tree

【代码】LeetCode //C - 655. Print Binary Tree

Made in Code 1058

小白水平理解面试经典题目LeetCode 655. Print Binary TreeTree

给定二叉树的 root ,构造一个 0 索引的 m x n 字符串矩阵 res 来表示树的格式化布局。对于已放置在矩阵中位置 res[r][c] 的每个节点,将其左子节点放置在 res[r+1][c-2。小美:小伙子,可以啊,这不仅逻辑感人,阅读理解也有俩下子, 不过要是照的不美可有你好看了!面试官:你可以解答这道”融合链表“的题目吗,来看看你对二叉树结构的理解。小白:您好,面试官,这回可以了吧,我终于可以开心练摄影技术为小美照相了!面试官:矮油,不错啊,不过你这能不能写个测试啊。

心安成长的码字书房 1171

655. Print Binary Tree(打印二叉树

题目描述 方法思路 public class Solution { //Memory Usage: 37.8 MB, less than 5.71% //Runtime: 3 ms, faster than 100.00% public List<List<String>> printTree(TreeNode root) { in...

IPOC_BUPT的博客 353

655. Print Binary Tree**

655. Print Binary Tree** https://leetcode.com/problems/print-binary-tree/ 题目描述 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 t...

珍妮的选择的博客 318

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 equal to the height of the given binary tree.The column number n should always be an odd number.The r

淡然坊 1469

Leetcode655. Print Binary Tree

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 ro

林剑的博客 752

[leetcode] 655. 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 n...

Learning from the mistakes 302

[leetcode] 655. Print Binary Tree @ python

原题 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...

闲庭信步 240

leetcode_655. Print Binary Tree

https://leetcode.com/problems/print-binary-tree/ 打印整棵二叉树 class Solution { public: int getTreeHeight(TreeNode* root) { return root==NULL?0:max(getTreeHeight(root->left)...

weixin_34297300的博客 116

LeetCode刷题测试辅助(更好的二叉树打印

这个题的要求就是打印一个二叉树,我们在此基础上进行修改,日后的二叉树问题,debug时就可以直接使用了,下面附上我的代码。

dodamce的博客 305

Leetcode-每日一题【剑指 Offer 32 - I. 从上到下打印二叉树

从上到下打印二叉树的每个节点,同一层的节点按照从左到右的顺序打印

lucky_1314520的博客 704

LeetCode 652. 寻找重复的子树

【DFS+哈希表】通过先序遍历获取所有子树的唯一表示(可以通过'#'来表示null,','来分割左右根,达到保留树结构的目的),将树的表示存入哈希表,在便利过程中查询是否在表中即可。

Sasakihaise_的博客 2763

[leetcode]652. Find Duplicate Subtrees

题目链接:https://leetcode.com/problems/find-duplicate-subtrees/description/ Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the ro

xiaocong1990的博客 1370

leetcode:652. 寻找重复的子树

那么我们只要使用一个哈希表存储所有子树的序列化结果,如果某一个结果出现了超过一次,我们就发现了一类重复子树。

OceanStar的博客 206

【小白向】leetcode中序遍历二叉树的三种实现方式

三种实现二叉树中序遍历的方法:递归遍历 + 闭包函数、显式栈模拟递归(颜色标记法)、递归调用 + 辅助函数。

weixin_60006489的博客 1483

基于STM32的智能线性位移传感器信号处理系统设计.zip

基于STM32的智能线性位移传感器信号处理系统设计

【10层】3427平米十层钢结构住宅楼(计算书、建筑、结构图).zip毕业设计参考建筑结构CAD计算书土木工程设计图纸资料库下载

【10层】3427平米十层钢结构住宅楼(计算书、建筑、结构图).zip毕业设计参考建筑结构CAD计算书土木工程设计图纸资料库下载【10层】3427平米十层钢结构住宅楼(计算书、建筑、结构图).zip毕业设计参考建筑结构CAD计算书土木工程设计图纸资料库下载【10层】3427平米十层钢结构住宅楼(计算书、建筑、结构图).zip毕业设计参考建筑结构CAD计算书土木工程设计图纸资料库下载【10层】3427平米十层钢结构住宅楼(计算书、建筑、结构图).zip毕业设计参考建筑结构CAD计算书土木工程设计图纸资料库下载1.合个人学习技术做项目参考合个人学习技术做项目参考2.适合学生做毕业设计项目参考适合学生做毕业设计项目技术参考 3.适合小团队开发项目技术参考适合小团队开发项目技术参考

上一篇: Leetcode 654. Maximum Binary Tree 最大二叉树 解题报告
下一篇: Tensorflow 机器翻译NMT笔记 1 快速上手
学术状态抽奖器
博客等级 码龄14年 249粉丝 270原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值