parents[node.left] = node
                queue.put(node.left)

            if node.right is not None:
                parents[node.right] = node
                queue.put(node.right)

        ancestor = set()
        p_cur = p
        while p_cur is not None:
            ancestor.add(p_cur)
            p_cur = parents[p_cur]

        cur = q
        while cur not in ancestor:
            cur = parents[cur]

        return cur


if __name__ == "__main__":
    from tools.construct_binary_tree import Construction

    test_tree = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]

    test_root = Construction(test_tree).construct()

    # print(Solution().lowestCommonAncestor(test_root, 5, 1).val)
    print(Solution().iteration(test_root, TreeNode(5), TreeNode(1)).val)

        return False

    def push(self, node):
        """
        push node and its left side node to stack recursively
        :param node:
        :return:
        """
        if node is None:
            return
        self.stack.put(node)
        self.push(node.left)


if __name__ == "__main__":
    # Your BSTIterator object will be instantiated and called as such:
    from tools.construct_binary_tree import Construction

    tree = [7, 3, 15, None, None, 9, 20]
    root = Construction(tree).construct()

    obj = BSTIterator(root)
    for i in range(5):
        param_1 = obj.next()
        param_2 = obj.hasNext()
        print(param_1)
        print(param_2)



        paths = []
        tmp = []
        self.find_path(root, paths, tmp)
        print(paths)
        res = 0
        for item in paths:
            tmp_str = "".join([str(element) for element in item])
            res += int(tmp_str)
        return res

    def find_path(self, root, paths, tmp):
        if root is None:
            return

        if root.left is None and root.right is None:
            # tmp.append(root.val)
            paths.append(tmp + [root.val])
            return
        else:
            self.find_path(root.left, paths, tmp + [root.val])
            self.find_path(root.right, paths, tmp + [root.val])


if __name__ == "__main__":
    from tools.construct_binary_tree import Construction
    data = [4, 9, 0, 5, 1]
    constructor = Construction(data)
    root = constructor.construct()

    Solution().sumNumbers(root)
Exemplo n.º 4
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import deque


class Solution:
    def levelOrder(self, root):
        if not root: return []
        queue, res = deque([root]), []

        while queue:
            cur_level, size = [], len(queue)
            for i in range(size):
                node = queue.popleft()
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
                cur_level.append(node.val)
            res.append(cur_level)
        return res


if __name__ == "__main__":
    from tools.construct_binary_tree import Construction

    values = [3, 9, 20, None, None, 15, 7]
    root = Construction(values).construct()

    print(Solution().levelOrder(root))