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

  • 思路:

  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. python 最小二乘 leastsq 函数实现

    代码修改自 http://www.cnblogs.com/NanShan2016/p/5493429.html 网上百度了一下,主要是两个例子,一个利用了多项式函数,一个就是这个.有些细节没看懂,主要 ...

  2. codeforces626F

    CF626F Group Projects  有n个学生,每个学生有一个能力值ai.现在要把这些学生分成一些(任意数量的)组,每一组的“不和谐度”是该组能力值最大的学生与能力值最小的学生的能力值的差. ...

  3. [bzoj 4887] [Tjoi2017]可乐

    传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...

  4. Nginx之web服务器

    Nginx的介绍 Nginx是由俄罗斯的Igor Sysoev使用C语言开发的轻量级.高性能.开源.跨平台的Web服务器. Nginx使用基于事件驱动的架构能够并发处理百万级的TCP连接,高模块化的设 ...

  5. Python自学笔记(九)

    #类 #类的创建 :class类名 + 冒号,后面语句要缩进 #类的属性创建:通过赋值语句(即定义“是怎样的”) #实例方法的创建:def + 方法名(self) #方法具体的执行过程,即定义“能做什 ...

  6. es6对象复制合并 Object.assign

    对象的复制 var obj= { a: 1 }; var copy = Object.assign({}, obj); console.log(copy); //{ a: 1 } 对象的合并和封装 v ...

  7. python代码-leetcode1 两数相加

    1.两个循环 class Solution: def twoSum(self, nums, target): n=len(nums) for i in range(n): for j in range ...

  8. Linux中ctrl+z 、ctrl+c、 ctrl+d区别

    Ctrl + C 和Ctrl + Z都是中断命令,但是他们的作用却不一样. Ctrl + C 是强制中断程序的执行,进程已经终止. Ctrl + C 发送 SIGINT信号 参考:linux信号 Ct ...

  9. Springboot集成MapperFactory(ma.glasnost.orika.MapperFactory)类属性复制

    一.导入Jar() gradle方式 compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.5.1' maven方式 ...

  10. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_14-课程预览功能开发-CMS添加页面接口

    5.3 CMS添加页面接口 cms服务对外提供添加页面接口,实现:如果不存在页面则添加,否则就更新页面信息. 此接口由课程管理服务在课程预览时调用. 接口方法.:页面没有就添加.有了更新数据 之前的接 ...