Пример #1
0
    def inorderTraversal(self, root):
        list = []
        result = []
        temp = root
        while len(list) > 0 or temp:
            while temp:
                list.append(temp)
                temp = temp.left
            if len(list) > 0:
                temp = list[-1]
                result.append(temp.val)
                temp = temp.right
                del list[-1]

        return result


s = Solution()

test = [
    {
        "input": deserialize([1, None, 2, 3]),
        "output": 2
    },
]
for t in test:
    r = s.inorderTraversal(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
        r = s.inorderTraversal(t['input'])
Пример #2
0
        if root is None:
            return False
        return self.count(root, sum, 0)

    def count(self, root, sum, now):
        if root.left is None and root.right is None:
            if sum == now + root.val:
                return True
            else:
                return False
        elif root.right and root.left:
            return self.count(root.left, sum, now + root.val) or self.count(
                root.right, sum, now + root.val)
        elif root.right:
            return self.count(root.right, sum, now + root.val)
        else:
            return self.count(root.left, sum, now + root.val)


s = Solution()

test = [
    {
        "input": [deserialize([1, 2]), 1],
        "output": False
    },
]
for t in test:
    r = s.hasPathSum(t['input'][0], t['input'][1])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
Пример #3
0
class Solution:
    def pathSum(self, root, sum):
        self.result = []
        if root:
            self.add(root, sum, [])
        return self.result

    def add(self, root, all, num):
        num.append(root.val)
        if sum(num) == all:
            if root.left is None and root.right is None:
                self.result.append(copy.deepcopy(num))
        if root.left:
            self.add(root.left, all, num)
        if root.right:
            self.add(root.right, all, num)
        del num[-1]


s = Solution()

test = [
    {
        "input": [deserialize([-2, None, -3]), -5],
        "output": [[-2, -3]]
    },
]
for t in test:
    r = s.pathSum(t['input'][0], t['input'][1])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
Пример #4
0
        self.result = []
        if root:
            self.find(root, 0)
        return sum(self.result)

    def find(self, root, num):
        num = num * 10 + root.val
        if root.left is None and root.right is None:
            self.result.append(num)
        elif root.left is None:
            self.find(root.right, num)
        elif root.right is None:
            self.find(root.left, num)
        else:
            self.find(root.left, num)
            self.find(root.right, num)


s = Solution()

test = [
    {
        "input": deserialize([1, 2, 3, None, 5, None, None]),
        "output": 25
    },
]
for t in test:
    r = s.sumNumbers(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
        r = s.sumNumbers(t['input'])
            next = 0
            start = 0
            level = []
            while len(list) > start:
                tmp = list[start]
                if tmp.left:
                    list.append(tmp.left)
                if tmp.right:
                    list.append(tmp.right)
                level.append(tmp.val)
                if next == start:
                    r.append(level)
                    next = len(list) - 1
                    level = []
                start += 1
        return r


s = Solution()
null = None
test = [
    {
        "input": deserialize([1, 7, 0, 7, -8, null, null]),
        "output": 2
    },
]
for t in test:
    r = s.maxLevelSum(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
Пример #6
0
                max=self.dic[k]
        r=[]
        for k in self.dic:
            if self.dic[k]==max:
                r.append(k)
        return r
    def count(self,root):
        if root is None:
            return 0
        sum=root.val+ self.count(root.left)+ self.count(root.right)
        if sum not in self.dic:
            self.dic[sum]=1
        else:
            self.dic[sum] += 1
        return sum



s=Solution()

test=[
{"input":deserialize([5,2,-3]),"output": [2, -3, 4]},
{"input":deserialize([5,2,-5]),"output": [2]},
]
for t in test:
    r=s.findFrequentTreeSum(t['input'])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))
        r = s.findFrequentTreeSum(t['input'])

        while len(list) > 0 or temp:
            while temp:
                list.append(temp)
                temp = temp.left
            if len(list) > 0:
                temp = list[-1]
                result.append(temp)
                temp = temp.right
                del list[-1]
        sum = 0
        result.reverse()
        for node in result:
            sum += node.val
            node.val = sum
        return root


s = Solution()

test = [
    {
        "input": deserialize([5, 2, 13]),
        "output": 10
    },
]

for t in test:
    r = s.convertBST(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
            next = 0
            start = 0
            level = []
            while len(list) > start:
                tmp = list[start]
                if tmp.left:
                    list.append(tmp.left)
                if tmp.right:
                    list.append(tmp.right)
                level.append(tmp.val)
                if next == start:
                    r.append(level)
                    next = len(list) - 1
                    level = []
                start += 1
        return r


s = Solution()

test = [
    {
        "input": deserialize([3, 9, 20, None, None, 15, 7]),
        "output": [[3], [9, 20], [15, 7]]
    },
]
for t in test:
    r = s.levelOrder(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
        r = s.levelOrder(t['input'])
Пример #9
0
# Definition for a binary tree node.
from  util.tree import TreeNode,deserialize

class Solution:
    def sumOfLeftLeaves(self, root):
        if root is None:
            return 0
        if root.left is None :
            return self.sumOfLeftLeaves(root.right)
        elif root.left.left is None and root.left.right is None:
            return root.left.val+self.sumOfLeftLeaves(root.right)
        else:
            return self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)


s=Solution()

test=[
{"input":deserialize([0,2,4,1,None,3,-1,5,1,None,6,None,8]),"output":5},
{"input":deserialize([3,9,20,None,None,15,7]),"output":24},
]
for t in test:
    r=s.sumOfLeftLeaves(t['input'])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))
        r = s.sumOfLeftLeaves(t['input'])
        r=root
        for i in range(min(len(left),len(right))):
            if left[i]==right[i]:
                r=left[i]
        return r
    def find(self,root,p,parent):
        parent.append(root)
        if root is None:
            return False
        if root.val==p.val:
            return True
        if self.find(root.left,p,parent):
            return True
        else:
            del parent[-1]
        if self.find(root.right,p,parent):
            return True
        else:
            del parent[-1]

s=Solution()

test=[
{"input":[deserialize([3,5,1,6,2,0,8,None,None,7,4]),TreeNode(5),TreeNode(4)],"output": 6},

]
for t in test:
    r=s.lowestCommonAncestor(t['input'][0],t['input'][1],t['input'][2])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))
Пример #11
0
    def pathSum(self, root, sum):
        if root is None:
            return 0
        return self.add(root, sum) + self.pathSum(
            root.left, sum) + self.pathSum(root.right, sum)

    def add(self, root, sum):
        if root is None:
            return 0
        res = 0
        if sum == root.val:
            res = 1
        return res + self.add(root.left, sum - root.val) + self.add(
            root.right, sum - root.val)


s = Solution()
test = [
    {
        "input": [deserialize([1, -2, -3, 1, 3, -2, None, -1]), -1],
        "output": 4
    },
    {
        "input": [deserialize([10, 5, -3, 3, 2, None, 11, 3, -2, None, 1]), 8],
        "output": 3
    },
]
for t in test:
    r = s.pathSum(t['input'][0], t['input'][1])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
Пример #12
0
        if left is None and right is None:
            return True
        if left is None or right is None:
            return False
        if left.val != right.val:
            return False
        return self.isSymmetricLeftRight(
            left.left, right.right) and self.isSymmetricLeftRight(
                left.right, right.left)


s = Solution()

test = [
    {
        "input": deserialize([1, 2, 3, 3, None, 2, None]),
        "output": False
    },
    {
        "input": deserialize([1, None, 2, 3]),
        "output": False
    },
    {
        "input": deserialize([1, 2, 2, 3, 4, 4, 3]),
        "output": True
    },
    {
        "input": deserialize([1, 2, 2, None, 3, None, 3]),
        "output": False
    },
]
Пример #13
0
# Definition for a binary tree node.
from  util.tree import TreeNode,deserialize
class Solution(object):
    def isSubtree(self, s, t):
        if s is not None :
            return self.isSameTree(s,t) or self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
        else:
            return False
    def isSameTree(self, p, q):
        if p is None and q is None:
            return True
        elif p is not None and q is not None and p.val==q.val:
            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
        else:
            return False
null=None
s=Solution()
test=[
{"input":[deserialize([1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,2]),deserialize([1,null,1,null,1,null,1,null,1,null,1,2])], "output":2},

]
for t in test:
    r=s.isSubtree(t['input'][0],t['input'][1])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))
Пример #14
0

class Solution:
    def isBalanced(self, root):
        h, t = self.count(root)
        return t

    def count(self, root):
        if root:
            left, isleft = self.count(root.left)
            right, isright = self.count(root.right)
            if isleft and isright and abs(left - right) <= 1:
                return max(left, right) + 1, True
            else:
                return max(left, right) + 1, False
        return 0, True


s = Solution()

test = [
    {
        "input": deserialize([1, 2, 2, 3, 3, None, None, 4, 4]),
        "output": False
    },
]
for t in test:
    r = s.isBalanced(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
        r = s.isBalanced(t['input'])
Пример #15
0
from  util.tree import TreeNode,deserialize
class Solution:
    def maxPathSum(self, root):
        if root == None:
            return 0
        Max = -10000000
        r,Max=self.maxpath(root, Max)
        return Max

    def maxpath(self,root,Max):
        if root==None:
            return 0,Max
        l,Max = self.maxpath(root.left, Max)
        r,Max = self.maxpath(root.right, Max)
        Max=max(Max, max(0, l) + max(0, r) + root.val)
        return max(0,max(l,r))+root.val,Max

s=Solution()

test=[
{"input":deserialize([-10,9,20,None,None,15,7]),"output":42},
{"input":deserialize([1,2,3]),"output":6},
]
for t in test:
    r=s.maxPathSum(t['input'])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))

            if n==0:
                return self.countNodes(root.left)+self.countNodes(root.right)+1
            else:
                return n
        return 0
    def isFullTree(self,root):
        l,r=0,0
        nl,nr=root,root
        while nl:
            nl=nl.left
            l+=1
        while nr:
            nr = nr.right
            r += 1
        if l==r:
            return (1<<l)-1
        else:
            return 0

s=Solution()

test=[
{"input":deserialize([1,2,3,4,5,6]),"output": 6},

]
for t in test:
    r=s.countNodes(t['input'])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))