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根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
随机推荐
- Caffe初试
1.基本概念 Caffe是一个比较流行的神经网络框架,它支持C++.Python等语言,容易上手,但是代码貌似不怎么好读,等有空我...;) 2.Windows10下的部署 我把我Windows下的编 ...
- hdu4715
题解: 二分图判断 建立原图的补图 判断是否是二分图 代码: #include<cstdio> #include<cmath> #include<cstring> ...
- react-hooks: custom hooks
memberEntitiy: export interface MemberEntity { id: number; name: string; code: string; } const useMe ...
- canvas - 圆圈内 hover效果
链接
- Qt TabWidget QTabBar 宽高设置
/*************************************************************************** * Qt TabWidget QTabBar ...
- gcc编译 汇编 选项
gcc生成main.out的步骤分解:<blockquote>main.c-----(-S 编译)-------->main.s-------(-c 汇编)------->ma ...
- JavaScript define
1. AMD的由来 前端技术虽然在不断发展之中,却一直没有质的飞跃.除了已有的各大著名框架,比如Dojo,jQuery,ExtJs等等,很多公司也都有着自己的前端开发框架.这些框架的使用效率以及开发质 ...
- 用dwr封装表单项提交表单
首先,配置dwr环境,网上很多资料都说得很详细,这里就不写了. dwr封装form表单项,需要用到dwr定义的一个js方法:DWRUtil.getValues(yourform),这个方法可以返回一个 ...
- nginx 安装echo模块
学习资源: https://www.cnblogs.com/xwupiaomiao/p/7997938.html https://blog.csdn.net/hb1707/article/detail ...
- 借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input
发送请求代码 post.php <?php $url='http://localhost/web/curl/url.php'; $data='a=123|b=2&c=3'; $heade ...