Skip to content

Instantly share code, notes, and snippets.

@vigack
Last active November 30, 2017 07:08
Show Gist options
  • Save vigack/07e6e76205a85870b194b443c0b6385d to your computer and use it in GitHub Desktop.
Save vigack/07e6e76205a85870b194b443c0b6385d to your computer and use it in GitHub Desktop.
find height of a binary tree without recursion
"""
基本思想:利用层序遍历,浮标法记录最大值
Node struct:
class Node:
def __init__(self, data, left, right):
self.data = data
self.left = left
self.right = right
"""
def findHeight(root):
queue = [(root, 0)]
ml = 0;
while queue:
curr = queue.pop(0)
node = curr[0]
cl = curr[1]
if cl > ml:
ml = cl
if node.left:
queue.append((node.left, cl+1))
if node.right:
queue.append((node.right, cl+1))
return ml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment