I have a height method that able to find the height of Binary tree but not sure how to return the deepest node of a binary tree(multiple nodes if same depth).
BinaryNode.new(1,BinaryNode.new(2,leaf,leaf),BinaryNode.new(3,leaf,leaf))
where leaf represents empty
the height for this tree is 2 and the deepest nodes are 2,3 (same depth)
class BinaryNode
include Enumerable
def initialize(element,lchild,rchild)
@element, @lchild, @rchild = element, lchild, rchild
end
def deepestNode
if self.nil?
0
else
height1=@lchild.height+1
height2=@lchild.height+1
end
height=[height1,height2].max
height
end
end
end
Assumptions:
Refactor:
Getting the elements: