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:

Input: Binary tree: [1,2,3,4]
1
/ \
2 3
/
4 Output: "1(2(4))(3)"

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:

Input: Binary tree: [1,2,3,null,4]
1
/ \
2 3
\
4 Output: "1(2()(4))(3)"

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. 对于树,第一步一定要想到用递归。
 public class Solution {
public String tree2str(TreeNode root) {
if (root == null) return "";
String rootValue = String.valueOf(root.val);
String leftValue = tree2str(root.left);
String rightValue = tree2str(root.right); if (leftValue.equals("") && rightValue.equals("")) {
return rootValue;
} else if (leftValue.equals("")) {
return String.format("%s()(%s)", rootValue, rightValue);
} else if (rightValue.equals("")) {
return String.format("%s(%s)", rootValue, leftValue);
} else {
return String.format("%s(%s)(%s)", rootValue, leftValue, rightValue);
}
}
}

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. 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  5. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  6. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

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

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

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

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

  10. [Algorithm] Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

随机推荐

  1. mysql随机取出n条数据

    SELECT * FROM  tableName  ORDER BY  RAND() LIMIT n      数据量小的话还可以, 数据量大起来了, 就影响性能了. $rubbish = (new ...

  2. 内存拷贝函数 memcpy

    windows下实现: void* __cdecl memcpy(void* dst,const void* src,size_t count) { void*ret=dst; #if defined ...

  3. 12.JSTL标签

    JSTL是一个不断完善的开源代码的JSP标签库,在JSP2.0中已将JSTL作为标准支持.使用JSTL可以取代在传统JSP程序中嵌入Java代码的做法,在一定程度上提高了代码的可维护性. JSTL有5 ...

  4. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

  5. [CSP-S模拟测试]:序列(二分答案+树状数组)

    题目传送门(内部题98) 输入格式 第一行一个整数$n$,第二行$n$个整数$a_1\sim a_n$,第三行$n$个整数$b_1\sim b_n$. 输出格式 一行一个整数表示$\max(r-l+1 ...

  6. 8.2 HTML表单提交

    一.form表单 <form>用于向服务器提交数据,比如账号密码 使用method="get" 提交数据 是常用的提交数据的方式 如果form元素没有提供method属 ...

  7. SNOI2017炸弹

    这个东西其实我是不太会的……但是勉强卡过去了. 首先肯定是建有向图,然后求每个节点能访问的节点个数,最裸的打法就是按照题意枚举建边然后tarjan缩点,用bitset记录一下访问节点,但是bitset ...

  8. Zookeeper 安装及命令行操作

    [参考文章]:[分布式]Zookeeper使用--命令行 [参考文章]:zookeeper的数据模型 [参考文章]:zookeeper ACL使用 1. 安装包下载 官方下载地址 选择一个具体的版本进 ...

  9. git一键push至github脚本

    ######################################################################### # File Name: push.sh # Aut ...

  10. ANTLR4将JSON翻译成XML

    实现功能:构建一个JSON到XML的翻译器. antlr4文件: grammar JSON; json : object | array ; object : '{' pair (',' pair)* ...