Sum of Left Leaves
Updated
Problem statement
You are given the root of a binary tree. A leaf is a node with no children. A left leaf is a leaf that is the left child of its parent.
Return the sum of the values of all left leaves. If there are none, return 0.
The root has no parent, so it is never a left leaf, even when it has no children.
Function
sumOfLeftLeaves(root: TreeNode) → intExamples
Example 1
root = [5,3,8,1,4,7]return = 8The leaves are 1, 4 and 7. Leaf 1 is the left child of 3 and leaf 7 is the left child of 8, so both count. Leaf 4 is a right child, so it does not. The answer is 1 + 7 = 8.
Example 2
root = [2,null,6,4,null,-3]return = -3Node 4 is a left child, but it has a child of its own, so it is not a leaf. Its left child -3 is a leaf, and it is the only left leaf. The answer is -3.
Example 3
root = [9]return = 0The root is a leaf, but it is nobody's child, so it is not a left leaf. There are no left leaves, and the sum is 0.
Constraints
- The number of nodes in the tree is in the range
[1, 10^4]. -1000 <= Node.val <= 1000
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
- Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Mirror a Binary TreeAMD · ONSITE INTERVIEWAMD interview questions
- Find a Node’s Level in an N-ary TreeMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Symmetric Binary TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Vertical Order Traversal of a Binary TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
Interview reports that mention this question
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)