Given two trees, I want to return true if they are equal in structure and in value and false otherwise.
Given the following class:
class TreeNode
attr_accessor :val, :left, :right
def initialize val, left = nil, right = nil
@val = val
@left = left
@right = right
end
end
complete the function
def binary_tree_compare(a, b)
My research led me to here. I have tried the ruby solution but to no avail Below is the code I have tried so far:
def binary_tree_compare a, b
for_check << [a, b]
while a,b = for_check.shift
return false unless a.children.count == b.children.count
break if a.children.empty?
a_children = a.children.sort
b_children = b.children.sort
return false unless a_children == b_children
0.upto(a_children.count - 1) do |i|
for_check << [a_children[i], b_children[i]]
end
end
return true
end
I expect the output to be true if TreeNodes a and b are equal in structure and in value and false otherwise.
I am getting a syntax error and this is the stack trace:
solution.rb:4: syntax error, unexpected ',', expecting keyword_do_cond or ';' or '\n'
while a,b = for_check.shift
^
solution.rb:12: syntax error, unexpected keyword_do_cond, expecting keyword_end
...0.upto(a_children.count - 1) do |i|
... ^~
olution.rb:15: syntax error, unexpected keyword_end, expecting end-of-input
end
^~~ ```