Ejemplo n.º 1
0
def main():
    import sys

    def readlines():
        for line in sys.stdin:
            yield line.strip('\n')

    # lines = readlines()
    while True:
        try:
            line = "[[2,4],[1,3],[2,4],[1,3]]"
            node = leetcode_utils.stringToInt2dArray(line)

            ret = Solution().cloneGraph(node)

            out = leetcode_utils.treeNodeToString(ret)
            print out
        except StopIteration:
            break
Ejemplo n.º 2
0
        pass

    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """

        if q.val < root.val and p.val < root.val:
            return self.lowestCommonAncestor(root.left, p, q)

        if q.val > root.val and p.val > root.val:
            return self.lowestCommonAncestor(root.right, p, q)

        return root


if __name__ == '__main__':
    line = "[6,2,8,0,4,7,9,null,null,3,5]"
    root = leetcode_utils.stringToTreeNode(line)
    line = "2"
    p = leetcode_utils.stringToInt(line)
    line = "8"
    q = leetcode_utils.stringToInt(line)

    ret = Solution().lowestCommonAncestor(root, p, q)

    out = leetcode_utils.treeNodeToString(ret)
    print out
Ejemplo n.º 3
0
                    rightNode = TreeNode(right_value)
                    node.right = rightNode
                    queue.append(rightNode)
            else:
                break

            index += 1
        return root


class Solution(object):
    def __init__(self):
        pass

    def func(self, root):
        pass


if __name__ == '__main__':
    treeLine = "[1,2,3,null,null,4,5]"
    empty = "[]"
    node = Codec().deserialize(empty)
    res = Codec().serialize(node)
    print res
    # res = Codec().deserialize(treeLine)
    # print res
    tree = leetcode_utils.stringToTreeNode(treeLine)
    mytree = Codec().deserialize(treeLine)
    # print Codec().serialize(tree)
    print leetcode_utils.treeNodeToString(mytree)
Ejemplo n.º 4
0
            # 删除操作
            # 让右子树的左叶子成为新的根
            if not root.left:
                return root.right

            elif not root.right:
                return root.left
            else:
                # 找到右子树的最小节点
                newNode = self.getMin(root.right)
                # 删除右子树的最小节点
                newNode.right = self.deleteMin(root.right)
                newNode.left = root.left
                return newNode
            pass
        elif root.val < key:
            # 在右树上删除
            root.right = self.deleteNode(root.right, key)
            return root
        else:
            # 在左树上删除
            root.left = self.deleteNode(root.left, key)
            return root

if __name__ == '__main__':
    line = "[5,3,6,2,4,null,7]"
    key = 3
    root = leetcode_utils.stringToTreeNode(line)
    out = Solution().deleteNode(root,key)
    res = leetcode_utils.treeNodeToString(out)
    print res
Ejemplo n.º 5
0
        :type root: TreeNode
        :rtype: None Do not return anything, modify root in-place instead.
        中序遍历后,发现 不满足这个条件:前一个元素 < 当前元素 < 下一个元素,即可进行交换(值交换,不需要节点交换)
        """
        self.__inOrderTree(root)
        temp = self.treeNode1.val
        self.treeNode1.val = self.treeNode2.val
        self.treeNode2.val = temp


    def __inOrderTree(self,root):
        if not root:
            return None

        self.__inOrderTree(root.left)
        if self.pre and self.pre.val > root.val:
            if not self.treeNode1:
                self.treeNode1 = self.pre

            # 每次保存当前的元素
            self.treeNode2 = root
        self.pre = root  # 保存了前一个元素
        self.__inOrderTree(root.right)

if __name__ == '__main__':
    line = "[3,1,4,null,null,2]"
    root = leetcode_utils.stringToTreeNode(line)

    Solution().recoverTree(root)
    res = leetcode_utils.treeNodeToString(root)
    print res