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.

Example 1:

  1. Input: Binary tree: [1,2,3,4]
  2. 1
  3. / \
  4. 2 3
  5. /
  6. 4
  7.  
  8. Output: "1(2(4))(3)"

  9. Explanation: Originallay it needs to be "1(2(4)())(3()())",
    but you need to omit all the unnecessary empty parenthesis pairs.
    And it will be "1(2(4))(3)".

Example 2:

  1. Input: Binary tree: [1,2,3,null,4]
  2. 1
  3. / \
  4. 2 3
  5. \
  6. 4
  7.  
  8. Output: "1(2()(4))(3)"

  9. Explanation: Almost the same as the first example,
    except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.

 题目标签:Tree
  这道题目给了我们一个二叉树,需要我们建立一个二叉树结构的string。并且要省去一些不需要的括号组合。比如一个node只有left,没有right,那么这个right的()就是不需要的;又或者一个node,既没有左边也没有右边,那么这时候left和right的()都不需要。
那么就一个点,有4种可能性:
1- 没有left, 也没有right: 直接返回它自己的值;
2- 有left, 也有right: 那么left 和 right 都需要括号,并且把left 和 right recursively 代入function;
3- 没有left, 但有right: 那么left 和 right 都需要括号,并且把right recursively 代入function;
4- 有left, 但没有right: 那么只有left 需要括号, 并且把left recursively 代入function。
 
 

Java Solution:

Runtime beats 53.70%

完成日期:06/29/2017

关键词:Tree

关键点:分析可能性,利用条件来控制括号

 
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution
  11. {
  12. public String tree2str(TreeNode t)
  13. {
  14. String res = "";
  15.  
  16. if(t == null)
  17. return res;
  18.  
  19. if(t.left == null && t.right == null)
  20. return res + t.val;
  21.  
  22. res += t.val;
  23.  
  24. // case 1: left is not null, right is not null;
  25. if(t.left != null && t.right != null)
  26. {
  27. // left child
  28. res += "(" + tree2str(t.left) + ")";
  29. // right child
  30. res += "(" + tree2str(t.right) + ")";
  31. }
  32. else if(t.left == null && t.right != null) // case 2: left is null, right is not null;
  33. {
  34. // left child
  35. res += "(" + ")";
  36. // right child
  37. res += "(" + tree2str(t.right) + ")";
  38. }
  39. else if(t.left != null && t.right == null) // case 3: left is not null, right is null;
  40. {
  41. // left child
  42. res += "(" + tree2str(t.left) + ")";
  43. }
  44.  
  45. return res;
  46. }
  47. }

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

 

LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)的更多相关文章

  1. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  2. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  7. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  8. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  9. [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 ...

随机推荐

  1. 内置open()函数对外部文件的操作

    >>> file=open('c://333.csv','r') 一些基本打开关闭操作 >>> s=file.read() >>> print s ...

  2. 基于CSS UI开源框架汇总

    从16年数据统计就有20几款UI框架出现在市面上,至今为止能统计的框架应该有40款左右了.前端框架都是基于HMTL5.CSS.JS开发的,这里主要给大家聊一下CSS UI开源框架有哪些?以后工作中选择 ...

  3. 框架应用:Spring framework (一) - IoC技术

    IoC概念以及目标 IoC就是让原本你自己管理的对象交由容器来进行管理,其主要的目的是松耦合. IoC发展史 既然IoC的目标是为了松耦合,那它怎么做到的? 最后目标:降低对象之间的耦合度,IoC技术 ...

  4. org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc

    今天在Spring中换了一种配置bean的方式,发现报错了Unexpected exception parsing XML document from class path resource , 经过 ...

  5. 用wrk测试nginx/ndoejs/golang

    sudo taskset -c ./wrk -c1 -t1 -d30 http://localhost/hello wrk+nginx(helloworld module) sudo taskset ...

  6. Day4 装饰器——迭代器——生成器

    一 装饰器 1.1 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多 ...

  7. Django(博客系统):按照时间分层筛选“/blog/article/?create_time__year=2017”,出现问题:Database returned an invalid datetime value. Are time zone definitions for your database installed?

    问题背景 添加文章时间没问题,但为了设定博客文章按照时间分层筛选(创建时间的年份.年月&月份来搜索文章),我在blog这个django app的admin.py的ArticleAdmin类中做 ...

  8. 关于API,前后端分离

    之前再开放新型web项目和app时,遇到了和前后端交互的问题.总所周知的是,web前后端交接时,最重要的交互方式的接口的制定. 而关于接口的规定,衍生出了一大堆问题,第一是关于空值的制定,是不输出呢? ...

  9. JavaScript new Boolean(false) 其实是true

    Boolean类型是JavaScript原始数据类型(primitive type)之一:常用来表示 真或假,是或否:这个类型只有两个值:保留字true和false 一般用于控制语句:如下 if(Bo ...

  10. 洗礼灵魂,修炼python(5)--python操作符,内置函数

    前面提到了BIF(内置函数)这个概念,什么是内置函数,就是python已经定义好的函数,不需要人为再自己定义,直接拿来就可以用的函数,那么都有哪些BIF呢? 可以在交互式界面(IDLE)输入这段代码, ...