Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:38:35+00:00 2026-06-13T12:38:35+00:00

I have a height method that able to find the height of Binary tree

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:38:36+00:00Added an answer on June 13, 2026 at 12:38 pm

    Assumptions:

    • The Binary Tree is actually the root node
    • child_nodes returns the collection of child nodes as an array

    class BinaryNode
      attr_reader :element
    
      def initialize(element,lchild,rchild)
        @element, @lchild, @rchild = element, lchild, rchild
      end
    
      def height
        if @lchild.nil? && @rchild.nil?
          return 0
        else
         [@lchild, @rchild].collect {|n| n.nil ? 0 : n.height + 1 }.max      
        end
      end
    
      def deepest_nodes
        return [self] if self.height == 1
    
        [@lchild, @rchild].select {|n| !n.nil? && (n.height == self.height - 1)}.collect {|n| n.deepest_nodes}.flatten
      end
    end
    

    Refactor:

    class BinaryNode
      attr_reader :element
    
      def initialize(element,lchild,rchild)
        @element, @lchild, @rchild = element, lchild, rchild
      end
    
      def child_nodes
        [@lchild, @rchild].compact
      end
    
      def height
        if self.child_nodes.empty?
          return 0
        else
          self.child_nodes.collect {|n| n.height + 1 }.max      
        end
      end
    
      def deepest_nodes
        return [self] if self.depth == 1
    
        self.child_nodes.select {|n| n.height == self.height - 1}.collect {|n| n.deepest_nodes}.flatten
      end
    end
    

    Getting the elements:

    BinaryNode.new(1,BinaryNode.new(2,leaf,leaf),BinaryNode.new(3,leaf,leaf)).deepest_nodes.collect {|n| n.element } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple divs in a wrap, they have different height. I'd like to
I would like to have that the height of a canvas change automatically. When
I have set a fix height of DIV and set its overflow-y:scroll but the
I think this is a common problem... yet i have not been able to
Ok I have searched all over and I can't not seem to find the
the whole thing is , that with CopyFromScreen() Method i was able to compare
I have seen several similar questions to this, but none that addresses my specific
Let's say i have a method that get a GLuint texture as a parameter
I have a method that enables me to animate objects that have to do
I have an application where users are able to upload pictures in albums but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.