Skip to content

Commit e9aa902

Browse files
committed
solve problem Construct String From Binary Tree
1 parent 07cf89d commit e9aa902

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ All solutions will be accepted!
8383
|762|[Prime Number Of Set Bits In Binary Representation](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/description/)|[java/py/js](./algorithms/PrimeNumberOfSetBitsInBinaryRepresentation)|Easy|
8484
|521|[Longest Uncommon Subsequence I](https://leetcode-cn.com/problems/longest-uncommon-subsequence-i/description/)|[java/py/js](./algorithms/LongestUncommonSubsequenceI)|Easy|
8585
|812|[Largest Triangle Area](https://leetcode-cn.com/problems/largest-triangle-area/description/)|[java/py/js](./algorithms/LargestTriangleArea)|Easy|
86+
|606|[Construct String From Binary Tree](https://leetcode-cn.com/problems/construct-string-from-binary-tree/description/)|[java/py/js](./algorithms/ConstructStringFromBinaryTree)|Easy|
8687

8788
# Database
8889
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Construct String From Binary Tree
2+
This problem is easy to solve by recursion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public String tree2str(TreeNode t) {
12+
if (t == null) {
13+
return "";
14+
} else if (t.left == null && t.right == null) {
15+
return String.valueOf(t.val);
16+
} else if (t.right == null) {
17+
return t.val + "(" + tree2str(t.left) + ")";
18+
} else {
19+
return t.val + "(" + tree2str(t.left) + ")(" + tree2str(t.right) + ")";
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} t
10+
* @return {string}
11+
*/
12+
var tree2str = function(t) {
13+
if (t === null) {
14+
return ''
15+
} else if (t.left === null && t.right === null) {
16+
return `${t.val}`
17+
} else if (t.right === null) {
18+
return `${t.val}(${tree2str(t.left)})`
19+
} else {
20+
return `${t.val}(${tree2str(t.left)})(${tree2str(t.right)})`
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def tree2str(self, t):
10+
"""
11+
:type t: TreeNode
12+
:rtype: str
13+
"""
14+
if not t:
15+
return ''
16+
elif not t.left and not t.right:
17+
return str(t.val)
18+
elif not t.right:
19+
return str(t.val) + '(' + self.tree2str(t.left) + ')'
20+
else:
21+
return str(t.val) + '(' + self.tree2str(t.left) + ')' + '(' + self.tree2str(t.right) + ')'

0 commit comments

Comments
 (0)