Number of Nodes With Value One - Problem

There is an undirected connected tree with n nodes labeled from 1 to n and n - 1 edges. You are given the integer n.

The parent node of a node with a label v is the node with the label floor(v / 2). The root of the tree is the node with the label 1.

For example, if n = 7, then the node with the label 3 has the node with the label floor(3 / 2) = 1 as its parent, and the node with the label 7 has the node with the label floor(7 / 2) = 3 as its parent.

You are also given an integer array queries. Initially, every node has a value 0 on it. For each query queries[i], you should flip all values in the subtree of the node with the label queries[i].

Return the total number of nodes with the value 1 after processing all the queries.

Note that:

  • Flipping the value of a node means that the node with the value 0 becomes 1 and vice versa.
  • floor(x) is equivalent to rounding x down to the nearest integer.

Input & Output

Example 1 — Basic Tree
$ Input: n = 7, queries = [2,3,1]
Output: 1
💡 Note: Tree structure: 1 as root, 2,3 as children of 1, 4,5 as children of 2, 6,7 as children of 3. Query 2 flips subtree [2,4,5] → values: [0,1,0,1,1,0,0]. Query 3 flips subtree [3,6,7] → values: [0,1,1,1,1,1,1]. Query 1 flips entire tree → values: [1,0,0,0,0,0,0]. Only node 1 has value 1. Count = 1.
Example 2 — Single Query
$ Input: n = 3, queries = [2]
Output: 2
💡 Note: Tree: 1 as root, 2,3 as children. Query 2 flips subtree [2]. Final values: node 1=0, 2=1, 3=0. Only nodes 2 has value 1. Count = 1.
Example 3 — Multiple Flips
$ Input: n = 4, queries = [1,1,2]
Output: 2
💡 Note: Tree: 1 as root, 2,3 as children of 1, 4 as child of 2. Query 1 flips all nodes, query 1 again flips all nodes back, query 2 flips subtree [2,4]. Final values: node 1=0, 2=1, 3=0, 4=1. Count = 2.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ queries.length ≤ 105
  • 1 ≤ queries[i] ≤ n

Visualization

Tap to expand
Number of Nodes With Value One INPUT Binary Tree (n=7) 1 2 3 4 5 6 7 n = 7 queries = [2, 3, 1] 2 3 1 parent(v) = floor(v/2) ALGORITHM STEPS 1 Query 2: Flip subtree Nodes 2,4,5: 0-->1 2 4 5 2 Query 3: Flip subtree Nodes 3,6,7: 0-->1 3 6 7 3 Query 1: Flip ALL All nodes flip values 4 Count flips per node Odd flips = value 1 Flip Counts Node 1: 1 flip (odd) = 1 Node 2: 2 flips(even)= 0 Node 3: 2 flips(even)= 0 Node 4: 2 flips(even)= 0 Node 5: 2 flips(even)= 0 Node 6,7: 1 flip each= 1 FINAL RESULT Final Tree State 1 v=1 2 v=0 3 v=0 4 v=0 5 v=0 6 v=1 7 v=1 = Value 1 = Value 0 Output: 4 Nodes 1,6,7 + 1 more OK - 4 nodes with value 1 Key Insight: Instead of simulating each flip, count how many times each node gets flipped. A node v is flipped by query q if q is an ancestor of v (including v itself). Parent is floor(v/2). If total flips is ODD, final value = 1. Time: O(n * queries) for counting flips. Space: O(n) for storing flip counts. TutorialsPoint - Number of Nodes With Value One | Optimized - Count Flips per Node
Asked in
Google 25 Microsoft 20 Amazon 15
28.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen