本文整理汇总了Python中util.tree_node.TreeNode.right方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.right方法的具体用法?Python TreeNode.right怎么用?Python TreeNode.right使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类util.tree_node.TreeNode
的用法示例。
在下文中一共展示了TreeNode.right方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_case1
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def test_case1(self):
preorder = "abc"
inorder = "bac"
answer = TreeNode("a")
answer.left = TreeNode("b")
answer.right = TreeNode("c")
result = self.sol.buildTree(preorder, inorder)
compare = TreeNode.compare_tree(answer, result)
self.assertTrue(compare)
开发者ID:linglingithub,项目名称:testhello,代码行数:11,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py
示例2: helper
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def helper(self, left, right):
if left>right:
return None
if left==right:
return TreeNode(self.nums[left])
mid = (left+right) / 2
root = TreeNode(self.nums[mid])
root.left = self.helper(left, mid-1)
root.right = self.helper(mid+1, right)
return root
示例3: gen_tree
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def gen_tree(self, nums, left, right):
if left > right:
return None
if left == right:
return TreeNode(nums[left])
mid = left + int((right - left) / 2)
root = TreeNode(nums[mid])
root.left = self.gen_tree(nums, left, mid - 1)
root.right = self.gen_tree(nums, mid + 1, right)
return root
示例4: helper
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def helper(self, preorder, inorder, l1, r1, l2, r2):
if l1 > r1:
return None
root = TreeNode(preorder[l1])
idx = l2
while idx <= r2 and inorder[idx] != preorder[l1]:
idx += 1
lsize = idx - l2
root.left = self.helper(preorder, inorder, l1 + 1, l1 + lsize, l2, idx)
root.right = self.helper(preorder, inorder, l1 + lsize + 1, r1, idx + 1, r2)
return root
开发者ID:linglingithub,项目名称:testhello,代码行数:13,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py
示例5: generateBST
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def generateBST(self, head, start, end):
if start>end:
return None
mid = start + (end-start)/2
left = self.generateBST(head, start, mid-1)
root = TreeNode(head[0].val)
head[0] = head[0].next
right = self.generateBST(head, mid+1, end)
root.left = left
root.right = right
return root
示例6: build_max
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def build_max(self, A, start, end):
if start > end:
return None
idx = start
for i in range(start, end+1):
if A[i] > A[idx]:
idx = i
root = TreeNode(A[idx])
root.left = self.build_max(A, start, idx-1)
root.right = self.build_max(A, idx+1, end)
return root
示例7: build_tree_rec
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def build_tree_rec(self, root_idx, in_left, in_right):
if in_left > in_right:
return None
root = TreeNode(self.preorder[root_idx])
if in_left == in_right:
return root
in_root = self.inorder.index(self.preorder[root_idx])
left_length = in_root - in_left
right_length = in_right - in_root
root.left = self.build_tree_rec(root_idx+1, in_root-left_length, in_root-1)
root.right = self.build_tree_rec(root_idx+1+left_length,in_root+1,in_right)
return root
开发者ID:linglingithub,项目名称:testhello,代码行数:14,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py
示例8: arr_to_tree
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def arr_to_tree(self, arr, left, right):
if not arr:
return None
if left==right:
return TreeNode(arr[left])
if left>right:
return None
mid = (left+right)/2
root = TreeNode(arr[mid])
root.left = self.arr_to_tree(arr,left,mid-1)
root.right = self.arr_to_tree(arr,mid+1,right)
return root
示例9: helper
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def helper(self, inorder, left, right, postorder, pl, pr):
if left>right: #need this, otherwise wrong for case1
return None
if left == right:
return TreeNode(inorder[left])
root_val = postorder[pr]
root = TreeNode(root_val)
root_idx = inorder.index(root_val)
right_len = right-root_idx
root.left = self.helper(inorder, left, root_idx-1, postorder, pl, pr-right_len-1)
root.right = self.helper(inorder, root_idx+1, right, postorder, pr-right_len, pr-1)
return root
开发者ID:linglingithub,项目名称:testhello,代码行数:14,代码来源:construct_binary_tree_from_inorder_and_postorder_traversal.py
示例10: helper
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def helper(self, head, tail):
if head==tail:
return None
fast = head
slow = head
while fast.next != tail and fast.next.next!=tail:
fast = fast.next.next
slow = slow.next
root = TreeNode(slow.val)
root.left = self.helper(head, slow)
root.right = self.helper(slow.next, tail)
return root
示例11: to_bst
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def to_bst(self, head, tail):
if head is None or head==tail: # should move head==tail here, because tail is not included, otherwise wrong.
return None
elif head.next == tail:
return TreeNode(head.val)
fast = slow = head
while fast != tail and fast.next!=tail:
fast = fast.next.next
slow = slow.next
root = TreeNode(slow.val)
root.left = self.to_bst(head, slow)
root.right = self.to_bst(slow.next, tail)
return root
示例12: sortedListToBSTr
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def sortedListToBSTr(self, start, end):
if start > end:
return None
# mid
mid = (start + end) / 2
left = self.sortedListToBSTr(start, mid - 1)
root = TreeNode(Solution.h.val)
Solution.h = Solution.h.next_node
right = self.sortedListToBSTr(mid + 1, end)
root.left = left
root.right = right
return root
示例13: helper
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def helper(arr):
if len(arr) == 0:
return [None]
ret = []
for i in range(0, len(arr)):
left = helper(arr[:i])
right = helper(arr[i + 1:])
for j in left:
for k in right:
dummy = TreeNode(arr[i])
dummy.left = j
dummy.right = k
ret.append(dummy)
return ret
示例14: buildTree_mle
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def buildTree_mle(self, inorder, postorder): #memory limit exceed for case2
"""
:type inorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
if not inorder:
return None
if len(inorder) == 1:
return TreeNode(inorder[0])
root_val = postorder[-1]
root = TreeNode(root_val)
idx = inorder.index(root_val)
root.left = self.buildTree(inorder[:idx], postorder[0:idx])
root.right = self.buildTree(inorder[idx+1:], postorder[idx:-1])
return root
开发者ID:linglingithub,项目名称:testhello,代码行数:18,代码来源:construct_binary_tree_from_inorder_and_postorder_traversal.py
示例15: dfs
# 需要导入模块: from util.tree_node import TreeNode [as 别名]
# 或者: from util.tree_node.TreeNode import right [as 别名]
def dfs(self, start, end):
if start > end: return [None]
res = []
for rootval in range(start, end+1):
LeftTree = self.dfs(start, rootval-1)
RightTree = self.dfs(rootval+1, end)
for i in LeftTree:
for j in RightTree:
root = TreeNode(rootval)
root.left = i
root.right = j
res.append(root)
return res