LeetCode知识点总结 - 606
LeetCode 606. Construct String from Binary Tree
考点 | 难度 |
---|---|
String | Easy |
题目
Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
思路
1 没有left child,没有right child:不需要括号
2 只有left child:括号 + left child
3 只有right child:空括号 + 括号 + right child
4 两个都有:括号 + left child + 括号 + right child
因为3和4都需要两个括号,可以放在一个条件下写。
答案
public String tree2str(TreeNode t) {if(t==null)return "";if(t.left==null && t.right==null)return t.val+"";if(t.right==null)return t.val+"("+tree2str(t.left)+")";return t.val+"("+tree2str(t.left)+")("+tree2str(t.right)+")";
}
LeetCode知识点总结 - 606相关推荐
- LeetCode知识点总结 - 884
LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...
- LeetCode知识点总结 - 868
LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...
- LeetCode知识点总结 - 1413
LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...
- LeetCode知识点总结 - 2073
LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...
- LeetCode知识点总结 - 347
LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...
- LeetCode知识点总结 - 997
LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...
- LeetCode知识点总结 - 844
LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...
- LeetCode知识点总结 - 977
LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...
- LeetCode知识点总结 - 377
LeetCode 377. Combination Sum IV 考点 难度 DP Medium 题目 Given an array of distinct integers nums and a t ...
最新文章
- vlc linux将摄像头串流,vlc的应用之九:用vlc串流摄像头
- java编写地铁购票系统_Java_地铁购票系统
- tensorflow随笔 -tf.concat
- mysql adminer 导入csv_mysql导入csv的4种报错的解决方法
- linux7.0使用教程,CentOS7.0使用手册精编.pdf
- 作者:沈志宏(1977-),男,博士,中国科学院计算机网络信息中心高级工程师...
- 量子计算机有哪些战略意义,世界性颠覆!量子计算机在中国诞生,对我国有五层重大战略意义!...
- iOS Core Animation学习总结(2)--实现自定义图层
- 汇编语言 跳转到第一行输出黑底白字
- 未来3-4周可能出现大规模病毒或安全***事件
- tensorflow目标检测API实现血细胞图像识别和计数
- c语言编码任务描述,C语言委派任务问题代码及解析
- 【Web技术】1159- 浅析 Web 录屏技术方案与实现
- 中国信通院副院长王志勤:加快5G新基建,驱动数字化转型升级 (视频+全文+PPT)...
- MxNet系列——how_to——multi_devices
- Oracle EBS 动态调用 XML Publisher 模板 输出不同的报表
- Suspicious method call; should probably call draw rather than onDraw
- BZOJ 2277 strongbox (gcd)
- 贝叶斯规划学习BPL
- java 显示日历 swing_Java Swing 日历 控件