예제 #1
0
        def buildTree(l, r):
            if l > r:
                return None
            mid = (l + r) // 2
            root = TreeNode(mem[mid])
            root.left = buildTree(l, mid - 1)
            root.right = buildTree(mid + 1, r)

            return root
예제 #2
0
        def genTree(c):
            if c % 2 == 0:
                return []

            if c == 1:
                return [TreeNode()]
            out = []

            for i in range(1, c - 1, 2):
                left = genTree(i)

                for lt in left:
                    right = genTree(c - 1 - i)

                    for rt in right:
                        out.append(TreeNode(0, lt, rt))

            return out
예제 #3
0
        def buildSub(l1, r1, l2, r2):

            # print(l1, r1, l2, r2)

            if l1 > r1:
                return None
            root = TreeNode(preorder[l1])

            if l1 == r1:
                return root
            nr = preorder[l1 + 1]
            i = l2

            while postorder[i] != nr:
                i += 1
            root.left = buildSub(l1 + 1, l1 + 1 + (i - l2), l2, i)
            root.right = buildSub(l1 + 1 + (i - l2) + 1, r1, i + 1, r2 - 1)

            return root
예제 #4
0
from pytree import TreeNode


class Solution:
    def findTilt(self, root: TreeNode) -> int:
        def find(root):

            if root is None:
                return (0, 0)
            lo = find(root.left)
            ro = find(root.right)

            return (abs(lo[1] - ro[1]) + lo[0] + ro[0],
                    lo[1] + ro[1] + root.val)

        return find(root)[0]


root = '[1,2,3]'
root = '[4,2,9,3,5,null,7]'
root = '[21,7,14,1,1,2,2,3,3]'

root = TreeNode.fromStrList(root)
print(Solution().findTilt(root))
예제 #5
0
            out.append([])
            while len(stack1) > 0:
                node = stack1.pop()
                out[-1].append(node.val)
                if node.left != None:
                    stack2.append(node.left)
                if node.right != None:
                    stack2.append(node.right)
            if len(stack2) == 0:
                break
            out.append([])
            while len(stack2) > 0:
                node = stack2.pop()
                out[-1].append(node.val)
                if node.right != None:
                    stack1.append(node.right)
                if node.left != None:
                    stack1.append(node.left)
        return out


sl = Solution()
root = TreeNode(3)
root.left = TreeNode(9)
root.left.left = TreeNode(1)
root.left.right = TreeNode(3)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)
print(sl.zigzagLevelOrder(root))
예제 #6
0
                while len(queue) > 0:
                    tmp = queue.popleft()

                    if tmp is None:
                        continue

                    if tmp.val % 2 == 0:
                        return False

                    if tmp.val <= prev:
                        return False
                    prev = tmp.val

                    if tmp.left is not None:
                        queue2.append(tmp.left)

                    if tmp.right is not None:
                        queue2.append(tmp.right)
            queue = queue2

        return True


root = '[1,10,4,3,null,7,9,12,8,6,null,null,2]'
root = '[5,4,2,3,3,7]'
root = '[5,9,1,3,5,7]'
root = '[1]'
root = '[11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17]'

print(Solution().isEvenOddTree(TreeNode.fromStrList(root)))
예제 #7
0
from typing import *
from pytree import TreeNode

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        if root==None:
            return 0
        return 1+self.countNodes(root.left)+self.countNodes(root.right)

sl=Solution()
#     1
#    / \
#   2   3
#  / \  /
# 4  5 6
root=TreeNode(1)
root.left=TreeNode(2) 
root.left.left=TreeNode(4) 
root.left.right=TreeNode(5) 
root.right=TreeNode(3) 
root.right.left=TreeNode(6) 

print(sl.countNodes(root))
        
        min_val = root.val
        m = 12345678190000
        second = m

        def dfs(root):
            nonlocal second

            if root is None:
                return

            if root.val > min_val:
                second = min(second, root.val)
            else:
                dfs(root.left)
                dfs(root.right)

        dfs(root)

        return second if second != m else -1


sl = Solution()
root = '[2,2,5,null,null,5,7]'
root = '[2,2,2]'
root = '[1,1,3,1,1,3,4,3,1,1,1,3,8,4,8,3,3,1,6,2,1]'
root = '[2,2,3]'
root = TreeNode.fromStrList(root)
TreeNode.travel(root)
print(sl.findSecondMinimumValue(root))
예제 #9
0
class Solution:
    def increasingBST(self, root: TreeNode) -> TreeNode:
        out = None
        pre = None

        def midorder(root):
            if root is None:
                return
            midorder(root.left)
            nonlocal pre
            root.left = None

            if pre is not None:
                pre.right = root
            else:
                nonlocal out
                out = root
            pre = root
            midorder(root.right)

        midorder(root)

        return out


inp = '[2,1,4,null,null,3]'
root = TreeNode.fromStrList(inp)
sl = Solution()
out = sl.increasingBST(root)
TreeNode.travel(out)
예제 #10
0
            if r1.val != r2.val:
                return False

            return same(r1.left, r2.left) and same(r1.right, r2.right)

        def dfs(root):
            if root is None:
                return False

            if same(root, B):
                return True

            if dfs(root.left):
                return True

            return dfs(root.right)

        return dfs(A)


A = '[1,2,3]'
B = '[3,1]'

A = '[3,4,5,1,2]'
B = '[4,1]'

A = TreeNode.fromStrList(A)
B = TreeNode.fromStrList(B)
print(Solution().isSubStructure(A, B))