handle all testcases, Python O(n) time without HashMap

  • -1

    def checkEqualTree(self, root):
    :type root: TreeNode
    :rtype: bool
    if root.val == 0 and not root.left and not root.right: return False
    sumV = self.findSum(root)
    self.topRoot = root
    if sumV %2 != 0: return False
    self.ans = False
    return self.ans
    def findSum(self,root):
    if not root: return 0
    return self.findSum(root.left) + self.findSum(root.right) + root.val
    def checkTmpSum(self, root, checkSum):
    if not root: return 0
    left = self.findSum(root.left); right = self.findSum(root.right)
    if left + right + root.val == checkSum and root != self.topRoot: self.ans = True; return 0
    else: return self.checkTmpSum(root.left,checkSum) + self.checkTmpSum(root.right, checkSum) + root.val

    for handle testcase like [0,-1,1], I used topRoot to check.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.