
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
XOR of all the nodes in the sub-tree of the given node in C++
In this problem, we are given an n tree and there are some queries that are nodes of the tree. Our task is to print the XOR of all nodes of the sub-tree formed by the given node.
Let’s take an example to understand the problem,
Queries − {1, 6, 5}
Output −
0 0 5
Explanation −
1^6^3^2^4^7^5 6^2^4 5
To solve this problem, we will compute the xor of all nodes of the sub-tree by traversing the tree once and store it. Now, we will compute the xor of all nodes of the sub-tree if the child nodes and then computing for all given sub-trees. Storing the results saves time.
Example
Program to show the implementation of our solution,
#include <bits/stdc++.h> using namespace std; vector<vector<int> > graph; vector<int> values, xorValues; int computeXorValues(int i, int prev){ int x = values[i]; for (int j = 0; j < graph[i].size(); j++) if (graph[i][j] != prev) { x ^= computeXorValues(graph[i][j], i); } xorValues[i] = x; return x; } int solveQuerry(int u){ return xorValues[u]; } int main(){ int n = 7; graph.resize(n); xorValues.resize(n); graph[0].push_back(1); graph[0].push_back(2); graph[1].push_back(3); graph[1].push_back(4); graph[2].push_back(5); graph[2].push_back(6); values.push_back(1); values.push_back(2); values.push_back(3); values.push_back(4); values.push_back(5); values.push_back(6); values.push_back(7); computeXorValues(0, -1); int queries[] = { 0, 2, 4, 6 }; int q = sizeof(queries) / sizeof(queries[0]); for (int i = 0; i < q; i++) cout<<"Solution for querry "<<(i+1)<<": "<<solveQuerry(queries[i])<<endl; return 0; }
Output
Solution for querry 1: 0 Solution for querry 2: 2 Solution for querry 3: 5 Solution for querry 4: 7
- Related Articles
- Find sum of all nodes of the given perfect binary tree in C++
- XOR of the path between any two nodes in a Binary Tree in C++
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Program to find number of nodes in the sub-tree with the same label using Python
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++
- Number of leaf nodes in the subtree of every node of an n-ary tree in C++
- XOR of all the elements in the given range [L, R] in C++
- Python Program to Find the Sum of all Nodes in a Tree
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Print all nodes at distance k from a given node in C++
- Product of all leaf nodes of binary tree in C++
- Count the nodes in the given tree whose weight is a power of two in C++

Advertisements