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


Python TreeNode.fromArray方法代码示例

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


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

示例1: hasNext

# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import fromArray [as 别名]
        self.preOrderTraversal(root.left, dq)
        dq.appendleft(root)
        self.preOrderTraversal(root.right, dq)


    def hasNext(self):
        """
        :rtype: bool
        """
        return len(self.nodes) > 0

    def next(self):
        """
        :rtype: int
        """
        return self.nodes.pop().val


# Your BSTIterator will be called like this:
# i, v = BSTIterator(root), []
# while i.hasNext(): v.append(i.next())

nodes = TreeNode.fromArray([4,2,6,1,3,5,7])

v = BSTIterator(nodes[0])

while v.hasNext():
    n = v.next()
    print(n)

开发者ID:leochen4891,项目名称:interviewquestions,代码行数:31,代码来源:BinarySearchTreeIterator.py

示例2: Solution

# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import fromArray [as 别名]
#         self.left = None
#         self.right = None

from TreeNode import TreeNode
from collections import deque
class Solution(object):
    def kthSmallest(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: int
        """

        nodes = deque()
        self.preOrderTraversal(root, nodes)
        for n in nodes:
            print(n.val)
        return nodes[len(nodes)-k].val


    def preOrderTraversal(self,root, dq):
        if not root: return
        self.preOrderTraversal(root.left, dq)
        dq.appendleft(root)
        self.preOrderTraversal(root.right, dq)

s = Solution()
nodes = TreeNode.fromArray([1,None,2])

print(s.kthSmallest(nodes[0], 1))
开发者ID:leochen4891,项目名称:interviewquestions,代码行数:32,代码来源:KthSmallestElementInBST.py

示例3: TreeNode

# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import fromArray [as 别名]
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# Level order traversal, get rightmost node on each level
class Solution(object):
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        levels = list()
        self.levelOrderTraversal(root, 0, levels)
        return [level[len(level)-1] for level in levels]

    def levelOrderTraversal(self,root, depth, levels):
        if not root: return
        if depth >= len(levels): levels.append([])
        levels[depth].append(root.val)

        if root.left: self.levelOrderTraversal(root.left, depth+1, levels)
        if root.right: self.levelOrderTraversal(root.right, depth+1, levels)

head = TreeNode.fromArray([1,2,3,None,5,None,None])

s = Solution()
for n in s.rightSideView(head):
    print (n)
开发者ID:leochen4891,项目名称:interviewquestions,代码行数:33,代码来源:BinaryTreeRightSideView.py

示例4: len

# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import fromArray [as 别名]
        if depth >= len(levels): levels.append(None)
        if not root: return (False, False)
        hasP = False
        hasQ = False
        if root == p: hasP = True
        if root == q: hasQ = True
        leftHasP, leftHasQ = self.findPQ(root.left, p, q, depth+1, levels)
        rightHasP, rightHasQ = self.findPQ(root.right, p, q, depth+1, levels)
        pp = hasP or leftHasP or rightHasP
        qq = hasQ or leftHasQ or rightHasQ
        if pp and qq: levels[depth] = root
        return (pp,qq)

    def betterOne(self, root, p, q):
        if root in (None, p, q): return root
        left, right = (self.lowestCommonAncestor(kid, p, q) for kid in (root.left, root.right))
        return root if left and right else left or right



nodes = TreeNode.fromArray([3,5,1,6,2,0,8,None,None,7,4])
# for node in nodes:
#     if node: print(node.val)

s = Solution()
n5 = nodes[1]
n1 = nodes[2]
n4 = nodes[10]

# print(s.lowestCommonAncestor(nodes[0], n5, n1).val)
print(s.lowestCommonAncestor(nodes[0], n5, n4).val)
开发者ID:leochen4891,项目名称:interviewquestions,代码行数:33,代码来源:LowestCommonAncestorOfABinaryTree.py


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