↓Skip to main content
  1. LeetCode/

LeetCode 563: Binary Tree Tilt

·1 min· ·
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-27

  • Submissions: Time Limit Exceeded ×2 → Wrong Answer → Runtime Error ×2 → Wrong Answer ×3 → Accepted

Approach
#

A node’s tilt is the difference between the sum of all nodes in its left subtree and in its right subtree (Tree Template 1). A post-order recursion returns whole-subtree sums while adding each tilt to the answer.

My Attempts
#

The early versions missed the empty tree or recomputed in loops (TLE). Later errors were all in the return value: the recursion returned the children’s values rather than whole-subtree sums, so [4, 6] gave 0 instead of 6; after adding the node’s own value it still returned root.val + left child + right child, missing deeper nodes.

Solution
#

class Solution:
    def findTilt(self, root: Optional[TreeNode]) -> int:
        total = 0
        def subtree_sum(node):
            nonlocal total
            if not node:
                return 0
            l, r = subtree_sum(node.left), subtree_sum(node.right)
            total += abs(l - r)
            return node.val + l + r
        subtree_sum(root)
        return total

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • Return the whole subtree’s sum, not the children’s values.

Interview Explanation
#

A post-order pass returns each subtree’s sum and adds the difference of the two sums at every node. One pass, time O(n).

Related

LeetCode 543: Diameter of Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 543 note: the diameter may skip the root; return heights in post-order while updating the answer with left + right height at every node, counting edges
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves
LeetCode 226: Invert Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 226 note: swap every node’s children, using a simultaneous assignment so the already-changed side is not reused
LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop