题目要求

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. JdbcTemplate中queryForObject方法返回空结果或不正确结果数量的解决方法

    在使用Spirng提供的JdbcTemplate中名为queryForObject API进行数据库查询时有时会抛出如下异常: org.springframework.dao.EmptyResultD ...

  2. Python-Django-Djangorestframwork

    1 CBV源码分析(cbv和fbv) 1 在views中写一个类,继承View,里面写get方法,post方法 2 在路由中配置: url(r'^test/', views.Test.as_view( ...

  3. 初识C语言(一)

    C语言的结构体 一个C程序就是由多个头文件和函数组成 #include<stdio.h> /* 包含头文件*/ int main() { printf('"hello world ...

  4. python---循环双向链表实现

    这个方案里的尾插入,就使用了更高效的prev指标. 但感觉remove的代码不够和前面统一, 我有代码洁癖了???? # coding = utf-8 # 双向链表 class Node: # 双向链 ...

  5. python数据类型一:字符串

    Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello W ...

  6. POJ 1208 The Blocks Problem --vector

    http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...

  7. 虚拟机Oracle VM VirtualBox linux系统如何访问windows共享文件夹

    1. 在本机系统设置一个共享文件夹,用于与Ubuntu交互的区域空间.     2.右击状态栏上共享文件夹图标或菜单栏“设备-共享文件夹”,打开共享文件夹设置,如图示   3.点击共享文件夹设置框,右 ...

  8. Sorting It All Out (拓扑排序+floyd)

    An ascending sorted sequence of distinct values is one in which some form of a less-than operator is ...

  9. webpack配置antd的按需加载

    安装babel-plugin-import插件.下面方法二选一,都可以实现antd的按需加载. 一.配置webpack.config.js文件 { test: /.jsx?$/, exclude: / ...

  10. vim编辑器操作命令

    vim   [参数]   [文件 ..]         编辑指定的文件 或: vim   [参数]    -     从标准输入(stdin)读取文本 或: vim   [参数]    -t    ...