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.

题目要求用括号的形式表示一颗二叉树,如果右边的节点为空括号()可以省略,如果左节点为空,括号()不能省略。

  1. class Solution {
  2. public String tree2str(TreeNode t) {
  3. if (t == null) return "";
  4. String result = "" + t.val;//根节点
  5. String left = tree2str(t.left); //左边
  6. String right = tree2str(t.right); //右边
  7. if(left.equals("") && right.equals("")) return result; //叶节点
  8. if (left.equals("") ) return result + "()" + "(" + right + ")";
  9. if (right.equals("") ) return result + "(" + left + ")";
  10. return result + "(" + left + ")" + "(" + right + ")";
  11. }
  12. }
 
 
 
 

606. Construct String from Binary Tree的更多相关文章

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

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

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

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

  6. 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 ...

  7. 606. Construct String from Binary Tree 从二叉树中构建字符串

    [抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...

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

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

  9. Python 解LeetCode:606 Construct String from Binary Tree

    题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...

随机推荐

  1. AIO5销售发货单numeric算数溢出报错:将numeric转换成数据类型numeric时出现算数溢出错误

    问题描述: 销售发货单报错算数溢出:将numeric转换成数据类型numeric时出现算数溢出错误.具体如下图: 当销售发货单的数量为>7万时,报错 当销售发货单的数量为<7万时,单据正常 ...

  2. 安卓自定义控件(三)实现自定义View

    前面两篇博客,把View绘制的方法说了一下,但是,我们只在onDraw里面做文章,控件都是直接传入一个Context,还不能在布局文件里使用自定义View.这一篇博客,就不再讲绘制,在我们原先的基础上 ...

  3. .net中ThreadPool与Task的认识总结

    线程池和Task是多线程编程中两个经常使用的技术,大家在熟悉不过了.他们有什么关联关系?Task又是怎么工作的呢?估计很多时候会犯糊涂.通过翻阅资料,终于弄明白了,与大家分享一下.   工作线程与I/ ...

  4. Less 的使用方法

    Less 的使用方法 Less 可以直接在浏览器端运行(支持IE6+.Webkit.Firefox),也可以借助Node.js或者Rhino在服务端运行. Less是一种动态语言,无论是在浏览器端,还 ...

  5. 面试相关-转载-well,yzl——持续更新

    转载yl,yzl大神的面经,顺便自己复习一下专业课的内容 操作系统相关: 什么是进程, 什么是线程.它们之间的区别和联系. 进程管理内存资源+运行过程, 线程只管理运行过程, 线程要在进程提供的资源基 ...

  6. HTML 使用jQuery选中复选框 简易版

    <html><head>   <meta charset="utf-8">   <script src="jquery-1.7. ...

  7. J Query库

    J Query库 J Query选择器:与CSS选择器完全一致 J Query语法: (1)美元符定义J Query (2)选择符查询和HTML元素 (3)J Query带action方法执行对元素带 ...

  8. jquery 动态创建的元素,绑定事件无效之解决方法

    今天遇到一个问题,动态创建的元素,绑定事件无效,如下: js 代码如下: var OaddX = $('.detright div.duibi div.duibox ul li span'); // ...

  9. 作为新手,SEO要避免的五大误区

    越来越多人在做网站的时候关注的不是网站的界面,而是网站的seo排名.Seo其实没有我们相信中的那么简单,特别对于新手,在实际操作过程中很容易遇到一些误区,今天我们简单说说新手要避免的五大seo误区. ...

  10. 初识NumPy库-基本操作

    ndarray(N-dimensional array)对象是整个numpy库的基础. 它有以下特点: 同质:数组元素的类型和大小相同 定量:数组元素数量是确定的 一.创建简单的数组: np.arra ...