q = Queue()
        q.put(root)

        while not q.empty():

            ret = []

            size = q.qsize()
            while size > 0:

                cur = q.get()
                ret.append(cur.val)

                if cur.right != None:
                    q.put(cur.right)
                if cur.left != None:
                    q.put(cur.left)

                size -= 1
            rets.append(ret)
        
        return rets


root = TreeNode.gen([3, 9, 20, None, None, 15, 7])
print root

s = Solution()
rets = 
Пример #2
0
        print(k_x, parent_x.val)
        print(k_y, parent_y.val)

        if k_x == k_y:
            if parent_x != parent_y:
                return True

        return False

    def dfs(self, parent, cur, k, target):
        if cur is not None:
            if cur.val == target:
                return k, parent

            k_ans, parent_ans = self.dfs(cur, cur.left, k + 1, target)
            if k_ans is not None:
                return k_ans, parent_ans

            return self.dfs(cur, cur.right, k + 1, target)

        return None, None


root = TreeNode.gen([1, 2, 3, None, 4])
x = 2
y = 3

s = Solution()
print(s.isCousins(root, x, y))