• 题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接

  • 思路:

  1. 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串
  2. 把1中的根结点和左右子结点的字符串连接起来就是结果,其中需要注意:
    • 如果右子树存在值,左子树无论有没有值,都需要用()括起来
    • 如果右子树不存在值,左子树只有在存在值的时候才括起来
# 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 tree2str(self, t):
"""
:type t: TreeNode
:rtype: str
"""
if not t:
return ''
root = str(t.val)
left = self.tree2str(t.left)
right = self.tree2str(t.right)
if right:
return root + '(' + left + ')(' + right + ')'
else:
if left:
return root + '(' + left + ')'
else:
return root

Python 解LeetCode:606 Construct String from Binary Tree的更多相关文章

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

  2. LeetCode 606 Construct String from Binary Tree 解题报告

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

  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】606. Construct String from Binary Tree 解题报告(Python)

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

  8. [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 ...

  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. 【00NOIP提高组】单词接龙

    #include<bits/stdc++.h> using namespace std; ; int n,length; int vis[N]; string str[N]; inline ...

  2. label设置渐变时不显示纯英文纯数字字符串

    提出问题: 当对UILabel设置渐变color时,有点小问题.即:text为中文或中英混合字符串时显示正常,纯英文字符串不显示!!!   剖析问题: 经搜索了解到:在显示中文时,绘制渐变color的 ...

  3. Go by Example-变量

    在上一节中提到了值类型,但是他们都是写在输出语句里的,如果后面程序需要用的话就显得非常的不方便,所以更好的办法就是把它们定义成变量,然后在调用. 变量 和Python不同的是,Go语言中的变量是需要的 ...

  4. linux文件管理指令

    总述 所有指令都可以使用--help来查看说明 例如:cat --help -x 表示参数 1.cat:用于打印文件(cat -x filename) 参数: -n:由1开始对每行进行编号 -b:由1 ...

  5. 【翻译】JNA调用DLL

    一.前言 Jna调用的示范,基本包括了Java->C基本类型的转换,指针的转换等. 不过文章是2011年的,可能后面要查看下有什么改变. 二.原文 http://www.viaboxxsyste ...

  6. PCA与LDA

  7. Elasticsearch 7.x文档基本操作(CRUD)

    官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html 1.添加文档 1.1.指定文档ID PUT ...

  8. SM30维护视图屏蔽按钮与增加选择条件

    *---------------------------------------------------------------------- * TABLES/Structure *-------- ...

  9. Apache配置优化之开启GZip传输

    1.确保apache已经编译的模块里有mod_deflate模块 2.确保apache的配置文件里引入了压缩的模块 3.确保要开启Gzip压缩的虚拟主机配置里有如下配置,并重启apache服务:如果要 ...

  10. Mysql安装、查看密码、修改密码、初始化、修改字符类型

    安装mysql 参照python篇一键安装lnmp.安装完之后再按照下面修改密码,修改配置文件,否则安装的时候就修改配置文件会出错. 注意:这也是二进制安装mysql.另一种二进制安装容易出错,生产环 ...