当前位置: 首页>>代码示例>>Python>>正文


Python Tree.levelorderTraversal方法代码示例

本文整理汇总了Python中Tree.levelorderTraversal方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.levelorderTraversal方法的具体用法?Python Tree.levelorderTraversal怎么用?Python Tree.levelorderTraversal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tree的用法示例。


在下文中一共展示了Tree.levelorderTraversal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: isBalanced

# 需要导入模块: import Tree [as 别名]
# 或者: from Tree import levelorderTraversal [as 别名]
"""

import Tree

def isBalanced(bt):
    if bt is None:
        return True
    d = Tree.getHeight(bt.left) - Tree.getHeight(bt.right)
    if abs(d) > 1:
        return False
    else:
        return isBalanced(bt.left) and isBalanced(bt.right)

# Balanced tree
bt = Tree.createPerfectTree(3)
Tree.levelorderTraversal(bt)
print()
print(Tree.getHeight(bt))
print(isBalanced(bt))

# Unbalanced tree
bt = Tree.Node(0)
bt.left = Tree.Node(1)
bt.right = Tree.Node(2)
bt2 = Tree.Node(3)
bt2.left = bt
Tree.levelorderTraversal(bt2)
print()
print(Tree.getHeight(bt2))
print(isBalanced(bt2))
开发者ID:YuSheng05631,项目名称:CrackingInterview,代码行数:32,代码来源:4.1.py

示例2: sorted

# 需要导入模块: import Tree [as 别名]
# 或者: from Tree import levelorderTraversal [as 别名]
"""
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
"""

import Tree

def createMBST_a(ary, start, end):
    if end < start:
        return None
    mid = int((start + end) / 2)
    n = Tree.Node(ary[mid])
    n.left = createMBST_a(ary, start, mid - 1)
    n.right = createMBST_a(ary, mid + 1, end)
    return n

def createMBST(ary):
    return createMBST_a(ary, 0, len(ary) - 1)

ary = [0, 1, 2, 3, 4, 5, 6]
n = createMBST(ary)
Tree.levelorderTraversal(n)
开发者ID:YuSheng05631,项目名称:CrackingInterview,代码行数:23,代码来源:4.3.py


注:本文中的Tree.levelorderTraversal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。