题目要求

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

题目分析及思路

给出一个二叉树,要求创建一个字符串,以前序遍历的顺序包含括号和二叉树中的整数。空结点用空括号对"()"表示。需要注意的一点是:不影响一一对应关系的空括号对要忽略,即当结点的左孩子结点为空时不能忽略。可以使用递归的方法,给出三个条件:1)该结点为空;2)该结点的子结点为空;3)该结点的右孩子结点为空。

python代码

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

def tree2str(self, t: TreeNode) -> str:

if not t:

return ""

if not t.left and not t.right:

return str(t.val)+""

if not t.right:

return str(t.val)+"("+self.tree2str(t.left)+")"

return str(t.val)+"("+self.tree2str(t.left)+")("+self.tree2str(t.right)+")"

LeetCode 606 Construct String from Binary Tree 解题报告的更多相关文章

  1. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  2. LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  3. LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)

    题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  4. 【刷题笔记】LeetCode 606. Construct String from Binary Tree

    题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1.  null 直接转化为  () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...

  5. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  6. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  7. [LeetCode&Python] Problem 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  8. Python 解LeetCode:606 Construct String from Binary Tree

    题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...

  9. 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

随机推荐

  1. 【Hibernate】could not instantiate class.. from tuple] with root cause

    使用hibernate的过程中出现了这个问题,查询语句如下: String hql = "select new GoodsBean(id, name, price, proPic, sale ...

  2. Python--可迭代对象,迭代器,生成器

    记得在刚开始学Python的时候,看到可迭代对象(iterable).迭代器(iterator)和生成器(generator)这三个名词时,完全懵逼了,根本就不知道是啥意识.现在以自己的理解来详解下这 ...

  3. 使用 wordcloud 构建词云图

    from wordcloud import WordCloudfrom matplotlib import pyplot as pltfrom PIL import Imageimport numpy ...

  4. springboot配置文件priperties大全

    flyway.baseline-description 执行基线时标记已有Schema的描述. flyway.baseline-on-migrate 在没有元数据表的情况下,针对非空Schema执行迁 ...

  5. 如何新建PDF文档,新建PDF文档的方法

    新建PDF文件的话,有两种方式,一种是直接通过使用PDF编辑器http://bianji.xjpdf.com/来新建PDF文件,,还有一种就是将PDF文件转换成Word文件,然后在Word文件中添加, ...

  6. appJSON["window"]["navigationBarTextStyle"] 字段需为 black 或 white

    appJSON["window"]["navigationBarTextStyle"] 字段需为 black 或 white     改动成这个就可以了 &qu ...

  7. OrCAD Capture CIS 16.6 为原理图中的Off-Page Connector添加页面编号

    操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 为原理图中的Off-Page Connector添加页面编号 一般来 ...

  8. Java集合实现

    set: public class BSTSet<E extends Comparable<E>> implements Set<E> { private BST& ...

  9. Android 10开发者预览版功能介绍

    Android P的开发者预览版最亮眼的功能莫过于支持“刘海屏”等屏幕显示.同样在适配可折叠设备方面,Android Q的第一个开发者预览版也很“接地气”,谷歌早在去年11月就发布了对可折叠设备的支持 ...

  10. ReactiveCocoa - study

    //KVO值监控,当alertTip改变时调用, filter对alertTip值进行过滤,此处当alertTip存在而长度不为0时,执行suscribeNext方法,弹出提示 [[RACObserv ...