Sum Nodes with an Even-Valued Grandparent
Updated
Problem statement
Given a binary tree, return the sum of every node whose grandparent exists and has an even value.
A grandparent is the parent of a node's parent. Nodes at depth zero or one never contribute.
Function
sumEvenGrandparent(root: TreeNode) → intExamples
Example 1
root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]return = 18The eligible descendants sum to 18.
Example 2
root = [1]return = 0The root has no grandparent.
Example 3
root = [2,1,3,4,5,6,7]return = 22All four grandchildren have the even root as grandparent.
Constraints
- The tree has 1 to 10000 nodes.
0 <= node.val <= 100.
More SambaNova Systems Onsite Interview questions
- Vectorized K-Means AssignmentsONSITE INTERVIEW · Seen Apr 2024
- 4Sum II Tuple CountONSITE INTERVIEW · Seen Jun 2022
- Bomb Enemy Best CoordinatesONSITE INTERVIEW · Seen Jun 2022
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jun 2022
- Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Maximum Area of IslandONSITE INTERVIEW · Seen Jun 2022
- Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Parse Variable, Expression, and Application TreesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Flip Equivalent Binary TreesONSITE INTERVIEW · Seen Oct 2021
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions