leetcode[Construct String from Binary Tree]//待整理多种解法
来源:互联网 发布:萌拍相机软件 编辑:程序博客网 时间:2024/06/08 13:22
解法一:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution {//根据题意,若结点的左子结点为空,则()不能省略private void recurse(TreeNode root, StringBuilder s){if(root == null) return;s.append("" + root.val);//没有孩子结点时直接返回if(root.left == null && root.right == null){return;}//左子树if(root.left == null){s.append("()");} else{s.append("(");recurse(root.left, s);s.append(")");}//右子树if(root.right == null){//什么也不做} else{s.append("(");recurse(root.right, s);s.append(")");}} public String tree2str(TreeNode t) { StringBuilder res = new StringBuilder(); recurse(t, res); return res.toString(); }}
阅读全文
1 0
- leetcode[Construct String from Binary Tree]//待整理多种解法
- leetcode [Balanced Binary Tree]//待整理多种解法
- leetcode [Minimum Depth of Binary Tree]//待整理多种解法
- leetcode [Invert Binary Tree]//待整理多种解法
- leetcode[Binary Tree Paths]//待整理多种解法
- leetcode[Diameter of Binary Tree]//待整理多种解法
- leetcode[Binary Tree Tilt]//待整理多种解法
- leetcode[Construct the Rectangle]//待整理多种解法
- Leetcode-536. Construct Binary Tree from String
- [leetcode]606. Construct String from Binary Tree
- [leetcode]: 606. Construct String from Binary Tree
- [LeetCode] 606. Construct String from Binary Tree
- LeetCode 606 Construct String from Binary Tree
- Leetcode 606. Construct String from Binary Tree
- LeetCode: 606. Construct String from Binary Tree
- [LeetCode]606. Construct String from Binary Tree
- leetcode 606. Construct String from Binary Tree
- LeetCode 606. Construct String from Binary Tree
- ServletContext 与application的异同
- leetcode 227. Basic Calculator II
- lua 字符串替换
- android 标题栏创建方法
- linux 回环网卡 实验
- leetcode[Construct String from Binary Tree]//待整理多种解法
- 基于ASP.NET Webapi和ajax技术且兼容Chrome、Firefox和IE浏览器的Excel文件下载方法
- 详解广度优先搜索(顺便学习C++的队列STL,也有一部分深搜的东西)
- ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
- JVM运行时数据区域及GC
- android 蓝牙各种UUID
- javascript中this使用场景的总结
- Android Binder 设计与实现
- 考博经历