コード例 #1
0
def remove_leaf_nodes(root: TreeNode, target: int) -> TreeNode:
    if root:
        root.left = remove_leaf_nodes(root.left, target)
        root.right = remove_leaf_nodes(root.right, target)
        if root.val == target and is_leaf(root):
            return None
        return root
コード例 #2
0
def prune_tree(root: TreeNode) -> TreeNode:
    if not root:
        return
    root.left = prune_tree(root.left)
    root.right = prune_tree(root.right)
    if not root.left and not root.right and root.val == 0:
        return None
    return root
コード例 #3
0
def prune_tree(root: TreeNode) -> TreeNode:
    if not root:
        return
    root.left = root.left if has_one(root.left) else None
    root.right = root.right if has_one(root.right) else None
    prune_tree(root.left)
    prune_tree(root.right)
    return root
コード例 #4
0
def insert_into_bst(root: TreeNode, val: int) -> TreeNode:
    if not root:
        return TreeNode(val)
    if root.val < val:
        root.right = insert_into_bst(root.right, val)
    if root.val > val:
        root.left = insert_into_bst(root.left, val)
    return root
コード例 #5
0
 def helper(inorder, postorder, in_st, in_end, post_end):
     if in_st > in_end or post_end < 0:
         return
     root = TreeNode(postorder[post_end])
     ind = inorder.index(postorder[post_end])
     root.left = helper(inorder, postorder, in_st, ind - 1,
                        post_end - 1 - (in_end - ind))
     root.right = helper(inorder, postorder, ind + 1, in_end, post_end - 1)
     return root
コード例 #6
0
 def helper(preorder: List[int], inorder: List[int], pre_st: int,
            in_st: int, in_end: int) -> TreeNode:
     if pre_st > len(preorder) or in_st > in_end:
         return
     root = TreeNode(preorder[pre_st])
     ind = inorder.index(preorder[pre_st])
     root.left = helper(preorder, inorder, pre_st + 1, in_st, ind - 1)
     root.right = helper(preorder, inorder, pre_st + 1 + (ind - in_st),
                         ind + 1, in_end)
     return root
コード例 #7
0
def construct(pre: List[int], post: List[int]) -> TreeNode:
    if not pre:
        return None
    root = TreeNode(pre[0])
    if len(pre) == 1:
        return root
    L = post.index(pre[1]) + 1
    root.left = self.constructFromPrePost(pre[1:L + 1], post[:L])
    root.right = self.constructFromPrePost(pre[L + 1:], post[L:-1])
    return root
コード例 #8
0
    def helper(i0, i1, N):
        if N == 0:
            return
        root = TreeNode(pre[i0])
        if N == 1:
            return root
        for L in range(N):
            if post[i1 + L - 1] == pre[i0 + 1]:
                break

        root.left = helper(i0 + 1, i1, L)
        root.right = helper(i0 + L + 1, i1 + L, N - (L + 1))
        return root
コード例 #9
0
def test_find_modes():
    n1 = TreeNode(1)
    n22 = TreeNode(2)
    n23 = TreeNode(2)
    n1.right = n22
    n22.left = n23
    fm1 = FindModes()
    assert fm1.find_modes(n1) == [2]

    n2 = TreeNode(2)
    n12 = TreeNode(1)
    n2.left = n12
    fm2 = FindModes()
    assert fm2.find_modes(n2) == [1, 2]
コード例 #10
0
    def allPossibleFBT(self, N: int) -> List[TreeNode]:
        if N not in self.memo:
            ans = []
            for x in range(N):
                y = N - 1 - x
                for left in self.allPossibleFBT(x):
                    for right in self.allPossibleFBT(y):
                        bns = TreeNode(0)
                        bns.left = left
                        bns.right = right
                        ans.append(bns)
            self.memo[N] = ans

        return self.memo[N]
コード例 #11
0
def bst_from_preorder(preorder: List[int]) -> TreeNode:
    if not preorder:
        return
    root_val = preorder[0]
    root = TreeNode(root_val)
    if len(preorder) == 1:
        return root
    index = get_index(preorder, root_val)
    if index:
        root.left = bst_from_preorder(preorder[1:index])
        root.right = bst_from_preorder(preorder[index:])
    else:
        root.left = bst_from_preorder(preorder[1:])
    return root
コード例 #12
0
    def helper(lo, hi):
        res = []
        if lo > hi:
            res.append(None)
            return res

        for rt in range(lo, hi + 1):
            leftlist = helper(lo, rt - 1)
            rightlist = helper(rt + 1, hi)

            for right in rightlist:
                for left in leftlist:
                    root = TreeNode(rt)
                    root.right = right
                    root.left = left
                    res.append(root)
        return res
コード例 #13
0
def tree1():
    root = TreeNode(0)
    root.left = TreeNode(3)
    root.right = TreeNode(0)
    return root