Tap here to switch tabs
Problem
Submissions

Problem: Binary Tree Tilt

Try to solve the Binary Tree Tilt problem.

Statement

Given the root of a binary tree, return the sum of the tilts of all nodes in the tree.

The tilt of a single tree node is defined as the absolute difference between the sum of all node values in its left subtree and the sum of all node values in its right subtree. If a node lacks a left child, the sum of the left subtree values is treated as 00. Similarly, if it lacks a right child, the sum of the right subtree values is treated as 00.

Constraints:

  • The number of nodes in the tree is in the range [0,104][0, 10^4].

  • −1000≤-1000 \leq Node.val ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Binary Tree Tilt

Try to solve the Binary Tree Tilt problem.

Statement

Given the root of a binary tree, return the sum of the tilts of all nodes in the tree.

The tilt of a single tree node is defined as the absolute difference between the sum of all node values in its left subtree and the sum of all node values in its right subtree. If a node lacks a left child, the sum of the left subtree values is treated as 00. Similarly, if it lacks a right child, the sum of the right subtree values is treated as 00.

Constraints:

  • The number of nodes in the tree is in the range [0,104][0, 10^4].

  • −1000≤-1000 \leq Node.val ≤1000\leq 1000