Question bank › Trees
Dsa Trees Warm-up

Tree Depth Sum

Given a binary tree, write a function to compute the sum of all node values multiplied by their depth. The depth of the root is 0, the depth of the root's children is 1, and so on. Input format: The first line contains an integer N, the number of nodes in the tree. The next N lines contain three integers: the node value, the left child value, and the right child value, where -1 indicates that the child does not exist. Output format: Output a single integer representing the total weighted sum of nodes. Example: Input: 3 1 2 3 2 -1 -1 3 -1 -1 Output: 3

Key concepts

treesdepthsum

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.