/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public string Tree2str(TreeNode t) {
if (t == null)
{
return "";
} string result = t.val + ""; string left = Tree2str(t.left);
string right = Tree2str(t.right); if (left == "" && right == "")
{
return result;
}
if (left == "")
{
return result + "()" + "(" + right + ")";
}
if (right == "")
{
return result + "(" + left + ")";
}
return result + "(" + left + ")" + "(" + right + ")";
}
}

https://leetcode.com/problems/construct-string-from-binary-tree/#/description

leetcode606的更多相关文章

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

  2. Leetcode606.Construct String from Binary Tree根据二叉树创建字符串

    你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...

随机推荐

  1. 24-THREE.JS 镜面高光材质

    <!DOCTYPE html> <html> <head> <title>Example 04.07 - Mesh Phong material< ...

  2. Solr集群安装

    1.JDK安装版本:jdk1.8.0 安装原文件路径:10.58.111.35(10.58.111.36.10.58.111.44)堡垒机 [/opt/jdk-8u101-linux-x64.tar. ...

  3. LeetCode OJ:Combination Sum (组合之和)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. unity 事件顺序及功能说明

    unity3d中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. Awake 当 ...

  5. 创建假的wifi热点

    本帖介绍怎么创建假的wifi热点,然后抓取连接到这个wifi用户的敏感数据.我们还会给周围的无线路由器发送未认证的包,使这些路由器瘫痪,强迫用户连接(或自动连接)我们创建的假wifi热点. 这种攻击也 ...

  6. Logical standby database 搭建(配置)

    说明 Logical standby 数据库是通过Physical standby数据库转换的.本Logical standby是通过之前创建的Physical standby转换的. Physica ...

  7. SGU 507 Treediff

    这个题目  其实可以暴力  用两个 set 合并: 每次放进去一个元素只要找到这个元素第一个比他大的元素和最后一个比他小的元素:然后更新最优值: 证明为什么不会超时:  假如最后集合的小的为 S1,大 ...

  8. c语言编译执行过程

    <h4>认识C编译执行过程</h4>认识C编译执行过程,是C学习的开端.简单说C语言从编码编译到执行要经历一下过程: C源代码编译---->形成目标代码,目标代码是在目标 ...

  9. linux rhel7下安装python

    1.查看是否已经安装Python Centos7默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用python -V命令查看一下是否安装Pytho ...

  10. Python Requests快速入门

    迫不及待了吗?本页内容为如何入门Requests提供了很好的指引.其假设你已经安装了Requests.如果还没有, 去 安装 一节看看吧. 首先,确认一下: Requests 已安装 Requests ...