-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtree_helper.rb
196 lines (159 loc) · 3.33 KB
/
btree_helper.rb
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
=begin
[]
[1,2,3,4,5]
[1]
[1,2,3,4,5,6,7,8]
[1,2,3,null,null,4]
[3,5,1,6,2,0,8,null,null,7,4]
[37,-34,-48,null,100,-100,48,null,null,null,null,-54,null,-71,-22,null,null,null,8]
[3,9,20,null,null,15,7]
=end
$btree_testcase = [
[1,2,3,4,5,6,7,8],
[],
[1,2,3,nil,nil,4],
[3,5,1,6,2,0,8,nil,nil,7,4],
[37,-34,-48,nil,100,-100,48,nil,nil,nil,nil,-54,nil,-71,-22,nil,nil,nil,8]
]
# Definition for a binary tree node.
class TreeNode
attr_accessor :val, :left, :right
def initialize(val)
@val = val
@left, @right = nil, nil
end
end
# Encodes a tree to a single string.
#
# @param {TreeNode} root
# @return {string}
def serialize(root)
q=[]
ans = []
return root if root.nil?
q.push(root)
until q.empty?
t = q.shift
if t.nil?
ans.push(nil)
else
ans.push(t.val)
if t.left.nil?
q.push(nil)
else
q.push(t.left)
end
if t.right.nil?
q.push(nil)
else
q.push(t.right)
end
end
end
ans
end
# Decodes your encoded data to tree.
#
# @param {string} data
# @return {TreeNode}
def deserialize(data)
return data if data.nil?
q=[]
root = TreeNode.new(data.shift)
q.push(root)
until data.empty?
t = q.shift
l = data.shift
r = data.shift
unless l.nil?
t.left = TreeNode.new(l)
q.push(t.left)
end
unless r.nil?
t.right = TreeNode.new(r)
q.push(t.right)
end
end
root
end
# @param {TreeNode} root
# @return {TreeNode}
def getNode(root, val)
return root if root.nil?
s=[]
s.push(root)
until s.empty?
r = s.pop
return r if r.val == val
s.push r.left unless r.left.nil?
s.push r.right unless r.right.nil?
end
return nil
end
# @param {TreeNode} root
def find_all_paths(root)
return [] if root.nil?
parent_path = Hash.new
s=[]
paths=[]
s.push(root)
until s.empty?
r=s.pop()
#puts r.val
#puts ''
p=''
if parent_path[r].nil?
p = r.val.to_s
else
#puts parent_path[r]
#puts r.val
p = "#{parent_path[r]}->#{r.val}"
end
#puts p
if r.left.nil? && r.right.nil?
#puts 'Its leafnode'
paths.push(p)
end
if !r.right.nil?
s.push(r.right)
parent_path[r.right]=p.to_s
end
if !r.left.nil?
s.push(r.left)
parent_path[r.left]=p.to_s
end
end
paths
end
# @param {TreeNode} root
# @param {TreeNode} node
def find_path(root, node)
return s if root.nil?
parent_path=Hash.new
s = []
s.push(root)
until s.empty?
r = s.pop()
current_path=[]
if parent_path[r].nil?
current_path.push(r.val)
else
current_path=parent_path[r].clone
current_path.push(r.val)
# print " node: #{r.val},parent: #{parent_path[r]}, current_path: #{current_path}\n"
end
#print "#{current_path}\n"
return current_path if node.val==r.val
if !r.right.nil?
#print "Push #{r.right} #{r.right.val}, parent_path: #{current_path}\n"
parent_path[r.right]=current_path
s.push(r.right)
end
if !r.left.nil?
#print "Push #{r.left} #{r.left.val}, parent_path: #{current_path}\n"
parent_path[r.left]=current_path
s.push(r.left)
end
end
return []
end