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


Python tree_node.TreeNode类代码示例

本文整理汇总了Python中util.tree_node.TreeNode的典型用法代码示例。如果您正苦于以下问题:Python TreeNode类的具体用法?Python TreeNode怎么用?Python TreeNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: test_case3

 def test_case3(self):
     nums = [6,5,8,1,3,7,11,None,None,None,None,None,None,9]
     answer = [6,3,8,1,5,7,11,None,None,None,None,None,None,9]
     root = TreeNode.generate_bt_from_list(nums)
     self.sol.recoverTree(root)
     compare = TreeNode.compare_tree(root, TreeNode.generate_bt_from_list(answer))
     self.assertTrue(compare)
开发者ID:linglingithub,项目名称:testhello,代码行数:7,代码来源:recover_binary_search_tree.py

示例2: test_case1

 def test_case1(self):
     nums = [3,1,2]
     answer = [2,1,3]
     root = TreeNode.generate_bt_from_list(nums)
     self.sol.recoverTree(root)
     compare = TreeNode.compare_tree(root, TreeNode.generate_bt_from_list(answer))
     self.assertTrue(compare)
开发者ID:linglingithub,项目名称:testhello,代码行数:7,代码来源:recover_binary_search_tree.py

示例3: test_case2

 def test_case2(self):
     nums = [1,2]
     root = TreeNode.generate_bt_from_list(nums)
     answer = [1,2]
     self.sol.flatten(root)
     result = TreeNode.get_tree_right_list(root)
     self.assertEqual(answer, result)
开发者ID:linglingithub,项目名称:testhello,代码行数:7,代码来源:flatten_binary_tree_to_linked_list.py

示例4: test_case1

 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,代码行数:9,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py

示例5: test_case11

 def test_case11(self):  #===>
     nums = "98,97,#,88,#,84,#,79,87,64,#,#,#,63,69,62,#,#,#,30,#,27,59,9,#,#,#,3,#,0,#,-4,#,-16,#,-18,-7,-19,#,#,#,-23,#,-34,#,-42,#,-59,#,-63,#,-64,#,-69,#,-75,#,-81"
     answer = "98,#,97,#,88,#,84,#,79,#,64,#,63,#,62,#,30,#,27,#,9,#,3,#,0,#,-4,#,-16,#,-18,#,-19,#,-23,#,-34,#,-42,#,-59,#,-63,#,-64,#,-69,#,-75,#,-81,#,-7,#,59,#,69,#,87"
     from util.tree_node import TreeNode
     root = TreeNode.generate_bt_from_string_standard(nums)
     answer_tree = TreeNode.generate_bt_from_string_standard(answer)
     self.sol.flatten(root)
     compare = TreeNode.compare_tree(root, answer_tree)
     self.assertTrue(compare)
开发者ID:linglingithub,项目名称:testhello,代码行数:9,代码来源:flatten_binary_tree+to_linked_list.py

示例6: helper

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:10,代码来源:convert_sorted_array_to_BST.py

示例7: gen_tree

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:10,代码来源:convert_sorted_array_to_BST.py

示例8: helper

 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,代码行数:11,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py

示例9: build_max

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:11,代码来源:max_tree.py

示例10: generateBST

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:11,代码来源:convert_linked_list_to_BST.py

示例11: build_tree_rec

 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,代码行数:12,代码来源:construct_binary_tree_from_preorder_and_inorder_traversal.py

示例12: arr_to_tree

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:12,代码来源:convert_sorted_list_to_binary_search_tree.py

示例13: maxTree

 def maxTree(self, A): # ref, use stack to track the decreasing part
     if not A:
         return None
     stack = []
     for i in range(len(A)):
         cur = TreeNode(A[i])
         while stack and stack[-1].val < cur.val:  # if use if here, wrong for case 1
             cur.left = stack.pop()
         if stack:
             stack[-1].right = cur
         stack.append(cur)
     return stack[0]
开发者ID:linglingithub,项目名称:testhello,代码行数:12,代码来源:max_tree.py

示例14: helper

 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,代码行数:12,代码来源:construct_binary_tree_from_inorder_and_postorder_traversal.py

示例15: helper

 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
开发者ID:linglingithub,项目名称:testhello,代码行数:12,代码来源:convert_linked_list_to_BST.py


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