示例#1
0
def test2():
    solution = Solution()
    tn1 = None
    tn21 = TreeNode(1)
    tn22 = TreeNode(2)
    tn21.left = tn22
    print(solution.isSameTree(tn1, tn21))
示例#2
0
 def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
     if not nums: return
     mid = len(nums) // 2
     root = TreeNode(nums[mid])
     root.left = self.sortedArrayToBST(nums[:mid])
     root.right = self.sortedArrayToBST(nums[mid + 1:])
     return root
示例#3
0
    def create_sub_tree(self, preorder, inorder):
        if not preorder:
            return None

        sub_tree_root_val = preorder[0]
        sub_tree_root = TreeNode(sub_tree_root_val)

        root_index = inorder.index(sub_tree_root_val)
        sub_tree_root.left = self.create_sub_tree(preorder[1:1 + root_index],
                                                  inorder[0:root_index])
        sub_tree_root.right = self.create_sub_tree(preorder[1 + root_index:],
                                                   inorder[root_index + 1:])

        return sub_tree_root
示例#4
0
 def sortedArrayToBSTNums(self, nums: List[int], start: int,
                          end: int) -> TreeNode:
     if end < start: return None
     n = end - start
     if n <= 0: return TreeNode(nums[start])
     if n == 1:
         a = TreeNode(nums[end])
         a.left = TreeNode(nums[start])
         return a
     n2 = start + int(n / 2)
     a = TreeNode(nums[n2])
     a.left = self.sortedArrayToBSTNums(nums, start, n2 - 1)
     a.right = self.sortedArrayToBSTNums(nums, n2 + 1, end)
     return a
示例#5
0
#

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
from src.tree.TreeNode import TreeNode

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        if root == None:return 0
        return 1 + self.countNodes(root.left) + self.countNodes(root.right)

s = Solution()
treeNode = TreeNode(4)
leftTreeNode = TreeNode(2)
leftLeftTreeNode = TreeNode(1)
leftRightTreeNode = TreeNode(3)
rightTreeNode = TreeNode(7)
rightLeftTreeNode = TreeNode(6)
leftTreeNode.left = leftLeftTreeNode
leftTreeNode.right = leftRightTreeNode
rightTreeNode.left = rightLeftTreeNode
treeNode.left = leftTreeNode
treeNode.right = rightTreeNode
print(treeNode)
flag = s.countNodes(leftTreeNode)
print(flag)
示例#6
0
def test3():
    solution = Solution()
    tn1 = TreeNode(1)
    tn11 = TreeNode(2)
    tn12 = TreeNode(2)
    tn1.left = tn11
    tn1.right = tn12

    tn2 = TreeNode(1)
    tn21 = TreeNode(2)
    tn22 = TreeNode(2)
    tn2.left = tn21
    tn2.right = tn22

    print(solution.isSameTree(tn1, tn2))
示例#7
0
def test2():
    tn0 = TreeNode(3)

    solution = Solution()
    print(solution.get_depth(tn0))
示例#8
0
def test1():
    tn0 = TreeNode(3)
    tn01 = TreeNode(9)
    tn02 = TreeNode(20)
    tn021 = TreeNode(15)
    tn022 = TreeNode(7)
    tn0.left = tn01
    tn0.right = tn02
    tn02.left = tn021
    tn02.right = tn022
    solution = Solution()
    print(solution.get_depth(tn0))
示例#9
0
def test1():
    solution = Solution()
    tn1 = TreeNode(3)
    tn11 = TreeNode(5)
    tn12 = TreeNode(1)
    tn111 = TreeNode(6)
    tn112 = TreeNode(2)
    tn1111 = TreeNode(7)
    tn1112 = TreeNode(4)
    tn121 = TreeNode(0)
    tn122 = TreeNode(8)
    tn1.left = tn11
    tn1.right = tn12
    tn12.left = tn121
    tn12.right = tn122
    tn11.left = tn111
    tn11.right = tn112
    tn112.left = tn1111
    tn112.right = tn1112
    print(solution.lowerCommonAncestor(tn1, tn11, tn12).val)