leetcode606
/**
* 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的更多相关文章
- [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 ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
随机推荐
- 【专题】区间dp
1.[nyoj737]石子合并 传送门:点击打开链接 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这 ...
- HDU 1561 The more, The Better(树形DP+01背包)
The more, The Better Time Limit : 6000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other ...
- 【机器学习基石笔记】九、LinearRegression
[一] 线性回归直觉上的解释 得到Ein = mean(y - wx)^2 [二] w的推导 Ein = 1/N || xw - y||^2 连续.可微.凸函数 在各个方向的偏微分都是0 Ein = ...
- Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...
- C++静态数据成员实现
静态数据成员是在一个类中用关键字static声明的数据成员.在C++中,一般使用静态成员来代替C语言的全局变量,以达到数据共享.C和C++的全局变量有一定的局限性,可以任意被修改,也容易和其它的变量名 ...
- 大白话讲解如何给github上项目贡献代码
本文献给对git很迷茫的新手,注意是新手,但至少会点基本操作,有点基本概念的新手,我不会从怎么用github和git是什么开始讲的.如果作为新手你看书又看不进去,原理又太复杂,又没有直接了当告诉我们怎 ...
- Nginx 日志分析及性能排查
Nginx 日志分析及性能排查 2017-03-04 Linux爱好者 (点击上方公众号,可快速关注) 作者:-外星人- my.oschina.net/362228416/blog/844713 如有 ...
- Ant入门之引用外部jar文件
笔者在java项目开发中经常遇到引用外部Jar包的情况,使用ant打包过程中需要对其引用.现在此简单记忆以飨来者. 此处引用Log4j,具体程序HelloLog4j.java: package oat ...
- HDU - 3949 :XOR(线性基,所有集合的不同异或和中,求从小到大第K个)
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A ...
- ZooKeeper初探之安装和配置
1. ZooKeeper简介 Zookeeper是Hadoop下的一个子项目,它是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护,名字服务,分布式同步,组服务等,Zookeeper是可 ...