Beispiel #1
0
def allPossibleFBT(N):
    table = [[]] + [[TreeNode(0)]] + [[] for _ in range(N)]
    #if N < 3:
    #  return table[N]

    for i in range(3, N + 1):
        # List of possible FBTs for `i` nodes, solves subproblem for N=i
        res = []

        # Iterate through all possible pairs of left/right idxs
        # Previous smaller subproblem solved and memoised in table
        for leftIdx in range(1, i - 1):
            # Require that leftIdx + rightIdx + 1 (root) = i
            rightIdx = i - 1 - leftIdx

            lefts = table[leftIdx]
            rights = table[rightIdx]

            for left in lefts:
                for right in rights:
                    # Build node and add ro
                    root = TreeNode(0)
                    root.left = left
                    root.right = right
                    res.append(root)

        table[i] = res

    return table[N]
Beispiel #2
0
def sortedListToBST(head):
    # base case: empty list
    if head is None:
        return None

    # base case: singleton list
    if head.next is None:
        return TreeNode(head.val)

    # use tortoise-hare algorithm to find midpoint, keeping track of mid's prev
    prev, left, right = None, head, head
    while right.next:
        # advance left pointer
        prev = left
        left = left.next

        # advance right pointer
        right = right.next
        if right.next is None:
            break
        right = right.next

    # partition w.r.t. midpoint defined by left
    prev.next = None

    # recursively build children
    leftChild = sortedListToBST(head)
    rightChild = sortedListToBST(left.next)

    # create, setup and return root node
    root = TreeNode(left.val)
    root.left = leftChild
    root.right = rightChild
    return root
Beispiel #3
0
def addOneRow(root, val, depth):
    # base case: empty tree, nothing to do
    if root is None:
        return root

    # base case: insert at root depth,
    # put current root as left subtree of new root as per spec
    if depth == 1:
        newRoot = TreeNode(val)
        newRoot.left = root
        return newRoot

    # setup level order traversal
    lvlToModify = depth - 1
    queue = deque([(root, 1)])

    while queue:
        node, lvl = queue.popleft()

        if lvl < lvlToModify:
            # add children as required
            if node.left:
                queue.append((node.left, lvl + 1))

            if node.right:
                queue.append((node.right, lvl + 1))

        else:
            # insert new row here
            newLeft = TreeNode(val)
            newLeft.left = node.left
            node.left = newLeft

            newRight = TreeNode(val)
            newRight.right = node.right
            node.right = newRight

    return root
Beispiel #4
0
def rangeSumBST(root, L, R):
    if not root:
        return 0

    val = root.val
    if val < L:
        return rangeSumBST(root.right, L, R)

    if val > R:
        return rangeSumBST(root.left, L, R)

    leftSum = rangeSumBST(root.left, L, R)
    rightSum = rangeSumBST(root.right, L, R)
    return leftSum + rightSum + val


if __name__ == "__main__":
    root = TreeNode(10)
    root.left = TreeNode(5)
    root.right = TreeNode(15)
    root.left.left = TreeNode(3)
    root.left.right = TreeNode(7)
    root.right.right = TreeNode(18)

    print(root)
    print("Enter space-separated range <L> <R>: ", end="")
    L, R = list(map(int, input().strip().split()))

    res = rangeSumBST(root, L, R)
    print(res)
Beispiel #5
0
    return empty_left and empty_right

  # Base case: root node is different, return False
  if root1.val != root2.val:
    return False

  r1_left = root1.left
  r1_right = root1.right
  r2_left = root2.left
  r2_right = root2.right

  return (flipEquiv(r1_left, r2_right) and flipEquiv(r1_right, r2_left)) or (flipEquiv(r1_left, r2_left) and flipEquiv(r1_right, r2_right))


if __name__ == "__main__":
  root1 = TreeNode(1)
  root1.left = TreeNode(2)
  root1.right = TreeNode(3)

  root2 = TreeNode(1)
  root2.left = TreeNode(3)
  root2.right = TreeNode(2)

  print("1: {}".format(root1))
  print("2: {}".format(root2))
  print(flipEquiv(root1, root2))

  root2.right = TreeNode(3)
  print("2: {}".format(root2))
  print(flipEquiv(root1, root2))
Beispiel #6
0
    return getLeaves(root1) == getLeaves(root2)


def getLeaves(root):
    # base case: empty tree
    if root is None:
        return []

    # base case: leaf
    if root.left is None and root.right is None:
        return [root.val]

    return getLeaves(root.left) + getLeaves(root.right)


if __name__ == "__main__":
    root1 = TreeNode(3)
    root1.left = TreeNode(5)
    root1.right = TreeNode(7)

    root2 = TreeNode(10)
    root2.left = TreeNode(1)
    root2.left.left = TreeNode(5)
    root2.right = TreeNode(7)

    print("Tree 1: {}".format(root1))
    print("Tree 2: {}".format(root2))

    similar = leafSimilar(root1, root2)
    print("Both trees are{} leaf similar.".format("" if similar else " not"))