One - One Code All

Blog Content

求二叉树的深度和宽度

Python 每日一练 算法   2011-01-02 10:03:01

用python写一下求二叉树深度和宽度的代码,求深度用递归;求宽度用队列,然后把每层的宽度求出来,找出最大的就是二叉树的宽度。

import queue

class Node:
    def __init__(self,value=None,left=None,right=None):
        self.value=value
        self.left=left
        self.right=right

def treeDepth(tree):
    if tree==None:
        return 0
    leftDepth=treeDepth(tree.left)
    rightDepth=treeDepth(tree.right)
    if leftDepth>rightDepth:
        return leftDepth+1
    if rightDepth>=leftDepth:
        return rightDepth+1

def treeWidth(tree):
    curwidth=1
    maxwidth=0
    q=queue.Queue()
    q.put(tree)
    while not q.empty():
        n=curwidth
        for i in range(n):
            tmp=q.get()
            curwidth-=1
            if tmp.left:
                q.put(tmp.left)
                curwidth+=1
            if tmp.right:
                q.put(tmp.right)
                curwidth+=1
        if curwidth>maxwidth:
            maxwidth=curwidth
    return maxwidth

if __name__=='__main__':
    root=Node('D',Node('B',Node('A'),Node('C')),Node('E',right=Node('G',Node('F'))))
    depth=treeDepth(root)
    width=treeWidth(root)
    print('depth:',depth)
    print('width:',width)



上一篇:消渴穴对治糖尿病
下一篇:python中pandas索引数据ix ,iloc ,loc 的区别

The minute you think of giving up, think of the reason why you held on so long.