def helper(lower=float('-inf'), higher=float('inf')):
     if not data or data[-1] < lower or data[-1] > higher:
         return None
     val = data.pop()
     root = TreeNode(val)
     root.right = helper(val, higher)
     root.left = helper(lower, val)
     return root
    def addOneRow(self, root, v, d):
        """
        :type root: TreeNode
        :type v: int
        :type d: int
        :rtype: TreeNode
        """
        if not root:
            return
        level = 0
        q = [root]
        if d == 1:
            t = root
            root = TreeNode(v)
            root.left = t
            return root
        z = []
        while q:
            level += 1
            if level == d - 1:
                z = z + q
            p = []
            for node in q:
                for leaf in (node.left, node.right):
                    #print leaf.val
                    if leaf:
                        p.append(leaf)
            q = p

        if level + 1 == d:
            for n in z:
                n.left = TreeNode(v)
                n.right = TreeNode(v)
        else:
            for change in z:
                if change.left:
                    t = change.left
                    change.left = TreeNode(v)
                    change.left.left = t
                if change.right:
                    t = change.right
                    change.right = TreeNode(v)
                    change.right.right = t
        return root
示例#3
0
    def longestConsecutive(self, root: TreeNode) -> int:
        self.cnt = 0
        self.helper(root, 1)
        return self.cnt

    def helper(self, root: TreeNode, parentCnt):
        if root == None:
            return
        if root.left != None:
            if (root.left.val > root.val):
                parentCnt += 1
                self.cnt = max(self.cnt, parentCnt)
                self.helper(root.left, parentCnt)
            else:
                self.helper(root.left, 1)
        if root.right != None:
            if (root.right.val > root.val):
                parentCnt += 1
                self.cnt = max(self.cnt, parentCnt)
                self.helper(root.right, parentCnt)
            else:
                self.helper(root.right, 1)


if __name__ == '__main__':
    S = Solution()
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(4)
    root.left.left = TreeNode(6)
    print(S.longestConsecutive(root))
示例#4
0

class Solution(object):
    def flipEquiv(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        if root1 is root2:
            return True
        if not root1 or not root2 or root1.val != root2.val:
            return False
        return (self.flipEquiv(root1.left, root2.left)
                and self.flipEquiv(root1.right, root2.right)) or (
                    self.flipEquiv(root1.left, root2.right)
                    and self.flipEquiv(root1.right, root2.left))


if __name__ == '__main__':
    S = Solution()

    root = TreeNode(5)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.right.right = TreeNode(4)
    root2 = TreeNode(5)
    root2.right = TreeNode(2)
    root2.left = TreeNode(3)
    root2.left.right = TreeNode(4)
    print(S.flipEquiv(root, root2))
示例#5
0
                preorder(root.left)
            if root.right:
                preorder(root.right)

        preorder(root)

        rightboundery = []
        curr = root.right
        if curr:
            while (curr.left or curr.right):
                rightboundery.append(curr.val)
                if curr.right:
                    curr = curr.right
                elif curr.left:
                    curr = curr.left
        rightboundery.reverse()
        result = result + rightboundery
        return result


if __name__ == '__main__':
    S = Solution()

    root = TreeNode(1)

    root.left = TreeNode(12)
    root.right = TreeNode(2)
    root.right.right = TreeNode(3)
    root.right.left = TreeNode(4)

    print(S.boundaryOfBinaryTree(root))