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

  • 思路:

  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. 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    分析:有向图里面找最短路径,原理就是每一步都走距离自己最近的路, 一旦发现走一步可以到,那么这个一定是最短的. #include <bits/stdc++.h> using namespa ...

  2. CSS 之实现单行、多行文本溢出显示省略号

    一.单行文本省略 实现方法: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 二.多行文本省略 实现方法: display ...

  3. CF463E Caisa and Tree

    要是你们能和我一样看错题目意思误认为是要求互质的就舒服了. 考虑修改很少,所以修改完之后可以暴力遍历树. 那么现在问题转换成了如何求一个点的答案,直接把所有质因子存下来然后用\(set\)维护即可. ...

  4. 个人学习分布式专题(二)分布式服务治理之Dubbo框架

    目录 Dubbo框架 1.1 Dubbo是什么 1.2 Dubbo企业级应用示例(略) 1.3 Dubbo实现原理及架构剖析 1.4 Dubbo+Spring集成 Dubbo框架 1.1 Dubbo是 ...

  5. OpenWrt笔记

    ## 1. OpenWrt目录结构说明 作者:辛勤的摆渡人 来源:CSDN 原文:https://blog.csdn.net/hunter168_wang/article/details/507805 ...

  6. gacutil.exe的位置

    如果我们需要用gacutil去注册dll ,就需要使用Visual Studio的Command Prompt,前提是需要安装Visual Studio,但是客户端上一般是没有安装VS的,所以你就需要 ...

  7. maven手动将jar包导入到本地仓库(支持多个仓库选择)

    正常我们在用maven搭建项目时,我们只需要将项目所需要的依赖配置到maven的配置文件pom.xml中即可,maven就可以去网上将jar包下载到配置的本地仓库中去.所以一般情况下我们是不需要手动安 ...

  8. 阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08

    阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08

  9. MongoDB基础笔记

    MongoDB show dbs 查看当前的数据库 use test 选库 show tables/collections 查看当前库下的文档 db.help() 查看帮助 db.createColl ...

  10. python线程池(转)

    ThreadPool: #! /usr/bin/env python # -*- coding: utf-8 -*- import threadpool import time def sayhell ...