Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接
思路:
- 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串
- 把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的更多相关文章
- 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 ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- 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 ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【Leetcode_easy】606. Construct String from Binary Tree
problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- [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 ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
随机推荐
- 微信小程序环境下将文件上传到 OSS
步骤 1: 配置 Bucket 跨域 客户端进行表单直传到 OSS 时,会从浏览器向 OSS 发送带有 Origin 的请求消息.OSS 对带有 Origin 头的请求消息会进行跨域规则(CORS)的 ...
- Python里面如何实现tuple和list的转换?
#list to tuple lis=[,,,,,] x=tuple(lis) print(type(x),x) #tuple to list tup=(,,,,,) y=list(tup) prin ...
- Smali语法基础
Smali是什么 Smali是Android虚拟机的反汇编语言. 我们都知道,Android代码一般是用java编写的,执行java程序一般需要用到java虚拟机,在Android平台上也不例外,但是 ...
- 作妖 | "该文件没有与之关联的程序来执行该操作..."的解决方法(删除快捷方式小箭头所致)
文章目录 开始作妖 后悔作妖 终结作妖 开始作妖 这是次很作妖的体验过程.因为重新换了个系统,看着桌面上这些快捷方式都有个讨人嫌的小箭头,就在网上搜了搜解决办法. 比如,将下面的内容复制到记事本中,再 ...
- 使用notepad++插件远程编辑linux下的配置文件
目录 1.安装插件管理器(Plugin Manager) 2.安装NppFTP 3.使用nppFTP连接远程linux服务器 1.安装插件管理器(Plugin Manager) 如果没有则需要安装 3 ...
- [转][C#]dll 引用
本文转自:https://zhidao.baidu.com/question/1176198151354174139.html 首先,对应关系: C++ C#===================== ...
- Android:Recents和AMS中历史任务的区别
1.1 任务和返回栈 - 实际数据模型 这个是指在调度体系里实际保存的TaskRecord实例,而ActivityRecord-TaskRecord-ActivityStack之间的关系建议看官方文 ...
- osgGA::KeySwitchMatrixManipulator 跟随
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osg/Group> #include <os ...
- APP 抓包-fiddler
App抓包原理 客户端向服务器发起HTTPS请求 抓包工具拦截客户端的请求,伪装成客户端向服务器进行请求 服务器向客户端(实际上是抓包工具)返回服务器的CA证书 抓包工具拦截服务器的响应,获取服务器证 ...
- 123457123457#0#-----com.yuming.ZuiNiuChengYu--前拼后广--最牛成语
com.yuming.ZuiNiuChengYu--前拼后广--最牛成语