bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 663. Equal Tree Partition

663. Equal Tree Partition

Leetcode Solutions

Depth-First Search to Find Equal Sum Partition

  1. Define a helper function to perform DFS on the tree and calculate the sum of each subtree.
  2. During DFS, store the sum of each subtree in a set or list.
  3. After DFS, check if the total sum of the tree is even, as an odd sum cannot be split into two equal parts.
  4. If the sum is even, check if half of the total sum exists in the set or list of subtree sums (excluding the total sum itself).
  5. If such a value exists, return true; otherwise, return false.
UML Thumbnail

Post-order Traversal with Subtree Sum Accumulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...