本文整理汇总了Python中Node.Node.addRight方法的典型用法代码示例。如果您正苦于以下问题:Python Node.addRight方法的具体用法?Python Node.addRight怎么用?Python Node.addRight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.addRight方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hasPathGreaterThanK
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addRight [as 别名]
hasPathGreaterThanK(node, k)
return node
def printTree(node):
level, q, res = 0, [[0,node]], {}
while len(q) != 0:
level, node = q.pop(0)
if level in res:
res[level] += [node.val]
else:
res[level] = [node.val]
if node.left != None:
q += [[level+1,node.left]]
if node.right != None:
q += [[level+1, node.right]]
return [res[k] for k in sorted(res.keys())]
n1, n2, n3, n4, n5, n6, n7, n8 = Node(1), Node(2), Node(3), Node(4), Node(5), Node(6), Node(7), Node(8)
n1.addLeft(n2)
n1.addRight(n3)
n2.addLeft(n4)
n2.addRight(n5)
n4.addLeft(n7)
n3.addRight(n6)
n6.addLeft(n8)
print printTree(n1)
print printTree(removeNodePathSmallerThanK(n1, 4))
示例2: preOrderT
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addRight [as 别名]
return res
def preOrderT(node):
if node == None:
return []
res = [node.val]
if node.left != None:
res += preOrderT(node.left)
if node.right != None:
res += preOrderT(node.left)
return res
def isSubtree(node1, node2):
node1_in, node1_pre = set(inOrderT(node1)), set(preOrderT(node1))
node2_in, node2_pre = set(inOrderT(node2)), set(preOrderT(node2))
return node1_in.issubset(node2_in) and node1_pre.issubset(node2_pre)
n1, n2, n3, n4 = Node("x"), Node("a"), Node("b"), Node("c")
n1.addLeft(n2)
n1.addRight(n3)
n2.addRight(n4)
N1, N2, N3, N4, N5, N6, N7 = Node("z"), Node("x"), Node("e"), Node("a"), Node("b"), Node("k"), Node("c")
N1.addLeft(N2)
N1.addRight(N3)
N2.addLeft(N4)
N2.addRight(N5)
N3.addRight(N6)
N4.addRight(N7)
print isSubtree(n1, N1)