We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Trees
  4. Tree: Height of a Binary Tree
  5. Discussions

Tree: Height of a Binary Tree

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • newbier
    3 months ago+ 0 comments

    Python 3, breadth-first traversal, without recursion

    def height(root):
        nodes = {0 : [root]}
        h = 0
        while nodes.get(h):
            nodes[h+1] = []
            for node in nodes[h]:
                if node.left:
                    nodes[h+1].append(node.left)
                if node.right:
                    nodes[h+1].append(node.right)
            h += 1
        return max(nodes.keys()) - 1
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy