Exemplo n.º 1
0
def sortedArrayToBST(nums):
    """
    :type nums: List[int]
    :rtype: TreeNode
    """
    le = len(nums)
    if le == 0:
        return None
    elif le == 1:
        return TreeNode(nums[0])
    else:
        idx = le/2
        root = TreeNode(nums[idx])
    handler(nums, root)
    return root

# root = sortedArrayToBST([1,3,4,5,6])
# print root
Exemplo n.º 2
0
def handler(nums, node):
    if len(nums) == 0:return
    mid = len(nums)/2
    l_s = nums[:mid]
    if len(l_s) > 0:
        l_mid = len(l_s)/2
        l_node = TreeNode(l_s[l_mid])
        node.left = l_node
        handler(l_s, l_node)
    else:
        return
    r_s = nums[mid+1:]
    if len(r_s) > 0:
        r_mid = len(r_s)/2
        r_node = TreeNode(r_s[r_mid])
        node.right = r_node
        handler(r_s, r_node)
    else:
        return
Exemplo n.º 3
0
    if leaf.left == leaf.right == None:
        if s == sum:
            return True
    else:
        if leaf.left:
            if handler(leaf.left, s, sum):
                return True
        if leaf.right:
            if handler(leaf.right, s, sum):
                return True
    return False


def PathSum(root, sum):
    if root is None:
        return False
    if root.left:
        if handler(root.left, root.val, sum):
            return True
    if root.right:
        if handler(root.right, root.val, sum):
            return True
    if root.left == root.right == None:
        return root.val == sum
    return False


root = TreeNode(1)
root.left = TreeNode(0)

print PathSum(root, 1)
    while True:
        left = root.left
        right = root.right
        if left is None and right is None:
            if len(stack) == 0:
                break
            root = stack.pop()
            max_path = max(max_path, ret)
            ret -= 1
        elif left:
            stack.append(root)
            root.left = None
            root = left
            ret += 1
        elif right:
            stack.append(root)
            root.right = None
            root = right
            ret += 1
    return max_path

leef = TreeNode(2)
leef.left = TreeNode(4)
leef.right = TreeNode(5)
leef.right.right = TreeNode(5)
leef.right.right.left = TreeNode(5)

root = TreeNode(1)
root.left = leef
root.right = TreeNode(3)
print maxDepth(root)
Exemplo n.º 5
0
                path += 1
            else:
                stack.append(root)
                root.right = None
                root = right
                path += 1
            level = level_dict.get(path)
            if level is None:
                level = [root.val]
                level_dict[path] = level
            else:
                level.append(root.val)
    ret = []
    idx = len(level_dict)
    while idx > 0:
        idx -= 1
        ret.append(level_dict.get(idx))
    return ret


leef = TreeNode(2)
# leef.left = TreeNode(4)
# leef.right = TreeNode(5)
# leef.right.right = TreeNode(5)
# leef.right.right.left = TreeNode(5)

root = TreeNode(1)
root.left = leef
root.right = TreeNode(3)
print levelOrderBottom(root)
            count = idx * 2
            if node.left:
                tmp[count] = node.left
            if m_node.right:
                tmp[pre_le * 2 - count - 1] = m_node.right
            count += 1
            if node.right:
                tmp[count] = node.right
            if m_node.left:
                tmp[pre_le * 2 - count - 1] = m_node.left
        pre_le *= 2
        stack = tmp
    return True


root = TreeNode(1)
root.left = TreeNode(3)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = None
root.right.left = None
root.right.right = TreeNode(4)
root.left.left = TreeNode(4)
root.left.right = None
root.right.left = None
root.right.right = TreeNode(4)
root.left.left.right = TreeNode(5)
root.right.right.left = TreeNode(5)
root.left.left.right.right = TreeNode(6)
root.right.right.left.left = TreeNode(6)
l = TreeNode(7)