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相关推荐

  1. LeetCode知识点总结 - 884

    LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...

  2. LeetCode知识点总结 - 868

    LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...

  3. LeetCode知识点总结 - 1413

    LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...

  4. LeetCode知识点总结 - 2073

    LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...

  5. LeetCode知识点总结 - 347

    LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...

  6. LeetCode知识点总结 - 997

    LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...

  7. LeetCode知识点总结 - 844

    LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...

  8. LeetCode知识点总结 - 977

    LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...

  9. LeetCode知识点总结 - 377

    LeetCode 377. Combination Sum IV 考点 难度 DP Medium 题目 Given an array of distinct integers nums and a t ...

最新文章

  1. vlc linux将摄像头串流,vlc的应用之九:用vlc串流摄像头
  2. java编写地铁购票系统_Java_地铁购票系统
  3. tensorflow随笔 -tf.concat
  4. mysql adminer 导入csv_mysql导入csv的4种报错的解决方法
  5. linux7.0使用教程,CentOS7.0使用手册精编.pdf
  6. 作者:沈志宏(1977-),男,博士,中国科学院计算机网络信息中心高级工程师...
  7. 量子计算机有哪些战略意义,世界性颠覆!量子计算机在中国诞生,对我国有五层重大战略意义!...
  8. iOS Core Animation学习总结(2)--实现自定义图层
  9. 汇编语言 跳转到第一行输出黑底白字
  10. 未来3-4周可能出现大规模病毒或安全***事件
  11. tensorflow目标检测API实现血细胞图像识别和计数
  12. c语言编码任务描述,C语言委派任务问题代码及解析
  13. 【Web技术】1159- 浅析 Web 录屏技术方案与实现
  14. 中国信通院副院长王志勤:加快5G新基建,驱动数字化转型升级 (视频+全文+PPT)...
  15. MxNet系列——how_to——multi_devices
  16. Oracle EBS 动态调用 XML Publisher 模板 输出不同的报表
  17. Suspicious method call; should probably call draw rather than onDraw
  18. BZOJ 2277 strongbox (gcd)
  19. 贝叶斯规划学习BPL
  20. java 显示日历 swing_Java Swing 日历 控件

热门文章

  1. 俄方产量也将削减恐左右油价未来走势
  2. GPT系列详解:初代GPT
  3. 阿里云ACP云计算工程师认证攻略--转载
  4. 机器学习初级算法之决策树
  5. Windows 7使用技巧大全(含键盘快捷键)
  6. 跨平台工具详解:Netbiscuits (Kony solutions的竞争对手)
  7. ORACLE进阶(十)start with connect by 实现递归查询
  8. php utf8(无bom),php中utf8 与utf-8 与utf8 无BOM
  9. Unix时间戳毫秒值解读
  10. Python实现倒计时程序