LeetCode 606 Construct String from Binary Tree 解题报告
题目要求
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 解题报告的更多相关文章
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- 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根据二叉树创建字符串 (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&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 ...
- Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
随机推荐
- 10张思维导图带你学习JavaScript
10张思维导图带你学习JavaScript 下面将po出10张JavaScript相关的思维导图. 分别归类为: JavaScript变量 JavaScript运算符 JavaScript数组 ...
- git提交忽略某些文件或文件夹
记得第一次用 github 提交代码,node_modules 目录死活传不上去,哈哈哈,后来才知道在 .gitignore 文件里设置了忽略 node_modules 目录上传.是的, .gitig ...
- Windows平台下结合 tortoiseSVN 和 VisualSVN Server 搭建SVN服务器并实现 web 站点同步
1. tortoiseSVN 关于 tortoiseSVN 的安装使用详见博文 TortoiseSVN的安装及其简单使用. 2. VisualSVN Server 关于 VisualSVN Serve ...
- JAVA 列表输入学生的信息
package Code429; import java.util.ArrayList; public class CodeArrayListStudent { public static void ...
- vue-标签页组件
content <template> <div class="tab-content"> <TabBar v-model="activeKe ...
- jupyter notebooks 中键盘快捷键
键盘快捷键——节省时间且更有生产力! 快捷方式是 Jupyter Notebooks 最大的优势之一.当你想运行任意代码块时,只需要按 Ctrl+Enter 就行了.Jupyter Notebooks ...
- IntelliJ IDEA运行eclipse的web项目报错的问题
用IDEA已经有一段时间了, 由于之前的IDEA版本不支持Tomcat服务器, 所以很长一段时间web项目都是由eclipse开发调试. 今天闲来无事下载了一个最新版的IDEA, 按网上的教程, 尝试 ...
- Windows Internals 笔记——进程的权限
1.大多数用户都用一个管理员账户来登录Windows,在Vista之前,这样的登录会创建一个安全令牌.每当有代码试图使用一个受保护的安全资源时,操作系统就会出示这个令牌.从包括Windows资源管理器 ...
- Emacs Org-mode 3 表格
Org 使用内置的表格编辑器.可以进行简单的表格编写和计算. Org中的表格 第一个非空字符“|” 视为表格的起始位置,后面的“|” 视为字段分隔符. 3.1 生成表格 编写表格时,可以将字段先写好, ...
- Conversation function
通过conversation function可以把类转成任意类型的值 #include <iostream> using namespace std; class Age { priva ...