-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path536-ConstructBinaryTreeFromString.go
More file actions
137 lines (123 loc) · 3.46 KB
/
536-ConstructBinaryTreeFromString.go
File metadata and controls
137 lines (123 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
// 536. Construct Binary Tree from String
// You need to construct a binary tree from a string consisting of parenthesis and integers.
// The whole input represents a binary tree.
// It contains an integer followed by zero, one or two pairs of parenthesis.
// The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
// You always start to construct the left child node of the parent first if it exists.
// Example 1:
// <img src="" />
// Input: s = "4(2(3)(1))(6(5))"
// Output: [4,2,6,3,1,5]
// Example 2:
// Input: s = "4(2(3)(1))(6(5)(7))"
// Output: [4,2,6,3,1,5,7]
// Example 3:
// Input: s = "-4(2(3)(1))(6(5)(7))"
// Output: [-4,2,6,3,1,5,7]
// Constraints:
// 0 <= s.length <= 3 * 10^4
// s consists of digits, '(', ')', and '-' only.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// dfs
func str2tree(s string) *TreeNode {
var dfs func(s string, l,r int) *TreeNode
dfs = func(s string, l,r int) *TreeNode{
if r < l {
return nil
}
node, flag := &TreeNode{}, 1
for l <= r {
if s[l]=='-' { // 处理 - 号
flag = -1
} else if s[l] != '(' {
node.Val = node.Val * 10 + int(s[l]-'0')
} else {
left, p := 1, l
for left > 0 {
if s[l+1] == '('{
left++
}else if s[l+1]== ')'{
left--
}
l++
}
node.Left = dfs(s, p+1, l-1)
node.Right = dfs(s, l+2, r-1)
break
}
l++
}
node.Val *= flag
return node
}
return dfs(s, 0, len(s)-1)
}
// stack bfs
func str2tree1(s string) *TreeNode {
if len(s) == 0 {
return nil
}
stack := make([]*TreeNode, 0)
for i := 0; i < len(s); i++ {
if s[i] == '(' {
continue
}
if s[i]== ')' { // 需要出栈了
stack = stack[:len(stack)-1]
continue
}
j, sign, num := i, 1, 0
if s[j] == '-' {
sign = -1
j++
}
for ; j <len(s) && s[j] >= '0' && s[j] <= '9'; j++ {
num = num * 10 + int(s[j]-'0')
}
i, num = j-1, num * sign
node:= &TreeNode { Val: num, }
if len(stack) > 0 {
tmp := stack[len(stack)-1]
if tmp.Left == nil {
tmp.Left = node
} else {
tmp.Right = node
}
}
stack = append(stack, node)
}
return stack[len(stack)-1]
}
func main() {
// Example 1:
// <img src="" />
// Input: s = "4(2(3)(1))(6(5))"
// Output: [4,2,6,3,1,5]
fmt.Println(str2tree("4(2(3)(1))(6(5))"))
// Example 2:
// Input: s = "4(2(3)(1))(6(5)(7))"
// Output: [4,2,6,3,1,5,7]
fmt.Println(str2tree("4(2(3)(1))(6(5)(7))"))
// Example 3:
// Input: s = "-4(2(3)(1))(6(5)(7))"
// Output: [-4,2,6,3,1,5,7]
fmt.Println(str2tree("-4(2(3)(1))(6(5)(7))"))
fmt.Println(str2tree1("4(2(3)(1))(6(5))"))
fmt.Println(str2tree1("4(2(3)(1))(6(5)(7))"))
fmt.Println(str2tree1("-4(2(3)(1))(6(5)(7))"))
}