Last active
November 30, 2017 07:08
-
-
Save vigack/07e6e76205a85870b194b443c0b6385d to your computer and use it in GitHub Desktop.
find height of a binary tree without recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
基本思想:利用层序遍历,浮标法记录最大值 | |
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