Check Whether a Tree Is a Sum Tree
Updated
Problem statement
Given the root of a binary tree, return true if it is a sum tree, and false otherwise.
In a sum tree, every node that has at least one child holds exactly the sum of all values below it: every value in its left subtree plus every value in its right subtree. A node is compared with everything below it, not just with its two children. A leaf (a node with no children) needs no check, so it may hold any value.
Function
isSumTree(root: TreeNode) → booleanExamples
Example 1
root = [26,10,3,4,6,null,3]return = true10 has 4 and 6 below it, and 4 + 6 = 10. 3 has only 3 below it. The root has 10 + 4 + 6 + 3 + 3 = 26 below it. Leaves need no check, so the answer is true.
Example 2
root = [12,5,1,2,3]return = false5 has 2 and 3 below it, which add up to 5. But the root 12 has 5 + 2 + 3 + 1 = 11 below it, not 12, so the answer is false.
Example 3
root = [-4]return = trueA single node is a leaf, and a leaf needs no check, so the answer is true whatever its value.
Constraints
- The tree has
1to500nodes. -2^31 <= Node.val <= 2^31 - 1
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Count Uni-Valued SubtreesAmazon · PHONE SCREEN
- Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
- Lowest Common Ancestor in a Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Height-Balanced Binary TreeExpedia · ONSITE INTERVIEWExpedia interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)