Skip to content

Commit 6fe5726

Browse files
committed
update
2 parents 636e27f + e7cd193 commit 6fe5726

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

C++/can-place-flowers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution {
1010
flowerbed[i] = 1;
1111
--n;
1212
}
13-
if (n <= 0) {
13+
if (n <= 0) {
1414
return true;
1515
}
1616
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Time: O(n)
2+
# Space: O(h)
3+
4+
# You need to construct a string consists of parenthesis and integers from a binary tree with
5+
# the preorder traversing way.
6+
#
7+
# The null node needs to be represented by empty parenthesis pair "()".
8+
# And you need to omit all the empty parenthesis pairs that don't affect
9+
# the one-to-one mapping relationship between the string and the original binary tree.
10+
#
11+
# Example 1:
12+
# Input: Binary tree: [1,2,3,4]
13+
# 1
14+
# / \
15+
# 2 3
16+
# /
17+
# 4
18+
#
19+
# Output: "1(2(4))(3)"
20+
#
21+
# Explanation: Originallay it needs to be "1(2(4)())(3()())",
22+
# but you need to omit all the unnecessary empty parenthesis pairs.
23+
# And it will be "1(2(4))(3)".
24+
# Example 2:
25+
# Input: Binary tree: [1,2,3,null,4]
26+
# 1
27+
# / \
28+
# 2 3
29+
# \
30+
# 4
31+
#
32+
# Output: "1(2()(4))(3)"
33+
#
34+
# Explanation: Almost the same as the first example,
35+
# except we can't omit the first parenthesis pair to break the one-to-one mapping relationship
36+
# between the input and the output.
37+
38+
39+
# Definition for a binary tree node.
40+
# class TreeNode(object):
41+
# def __init__(self, x):
42+
# self.val = x
43+
# self.left = None
44+
# self.right = None
45+
46+
class Solution(object):
47+
def tree2str(self, t):
48+
"""
49+
:type t: TreeNode
50+
:rtype: str
51+
"""
52+
if not t: return ""
53+
s = str(t.val)
54+
if t.left or t.right:
55+
s += "(" + self.tree2str(t.left) + ")"
56+
if t.right:
57+
s += "(" + self.tree2str(t.right) + ")"
58+
return s

0 commit comments

Comments
 (0)