winter camp 2
winter camp 2
winter camp 2
Problem: 1
1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (nodes.empty() || nodes[0] == "null") {
return nullptr;
}
return root;
}
int main() { string input; cout << "Enter the binary tree nodes in level order (use 'null' for
empty nodes, space-separated): "; getline(cin, input);
return 0;
}
4. Output:
Problem: 2
1. Aim: Binary Tree - Find Maximum Depth
2. Objective: A binary tree's maximum depth is the number of nodes along the longest path from the root
node down to the farthest leaf node.
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
return root;
}
4
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
string input;
cout << "Enter the binary tree nodes in level order (use 'null' for empty nodes, space-separated): ";
getline(cin, input);
return 0;
}
4. Output:
Problem: 3
6
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Create right child
if (index < nodes.size() && nodes[index] != "null") {
current->right = new TreeNode(stoi(nodes[index]));
q.push(current->right);
}
index++;
}
return root;
}
int main() {
string input;
cout << "Enter the binary tree nodes in level order (use 'null' for empty nodes, space-separated): ";
getline(cin, input);
return 0;
}
4. Output:
7
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem: 4
1. Aim: Invert Binary Tree
2. Objective: Given the root of a binary tree, invert the tree, and return its root.
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
8
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
TreeNode* invertBinaryTree(TreeNode* root) {
if (root == nullptr) {
}
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
index++;
}
index++;
}
return root;
}
queue<TreeNode*> q;
q.push(root); cout << "[";
while (!q.empty()) {
TreeNode* current = q.front();
q.pop(); if (current) {
cout << current->val << ",";
q.push(current->left);
10
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
q.push(current->right);
} else { cout <<
"null,";
}
}
cout << "\b]"; // Remove the last comma
}
int main() {
string input;
cout << "Enter the binary tree nodes in level order (use 'null' for empty nodes, space-separated): ";
getline(cin, input);
11
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout << endl;
return 0;
}
4. Output:
Problem: 5
1. Aim: Construct Binary Tree from Preorder and Inorder Traversal.
2. Objective: Given two integer arrays preorder and inorder where preorder is the preorder traversal of a
binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
3. Code:
#include <iostream>
#include <vector>
#include <queue> // Include the queue header
#include <unordered_map>
#include <sstream>
// Function to build the binary tree from preorder and inorder traversals
TreeNode* buildTreeHelper(vector<int>& preorder, int preStart, int preEnd,
12
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
vector<int>& inorder, int inStart, int inEnd,
unordered_map<int, int>& inMap) { if (preStart > preEnd || inStart >
inEnd) {
return nullptr; // Base case: no elements to construct the tree
}
int main() {
int n;
cout << "Enter the number of nodes: ";
cin >> n;
vector<int> preorder(n);
vector<int> inorder(n);
return 0;
}
4. Output:
14
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem: 6
1. Aim: Lowest Common Ancestor of a Binary Tree
2. Objective: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the
tree.
The lowest common ancestor is defined between two nodes p and q as the
lowest node in T that has both p and q as descendants (where we allow a node to
be a descendant of itself).
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
#include <unordered_map>
return root;
}
16
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (left && right) {
return root; // If both left and right are non-null, root is LCA
}
return left ? left : right; // Return the non-null child
}
int main() {
string input;
cout << "Enter the binary tree nodes in level order (use 'null' for empty nodes, space-separated): ";
getline(cin, input);
if (p && q) {
TreeNode* lca = lowestCommonAncestor(root, p, q); // Find the LCA
cout << "The LCA of nodes " << pVal << " and " << qVal << " is: " << lca->val << endl;
} else {
cout << "One or both of the nodes do not exist in the tree." << endl;
}
return 0;
17
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
4. Output:
Problem: 7
1. Aim: Sum Root to Leaf Numbers.
2. Objective: You are given the root of a binary tree containing digits from 0 to 9 only.
• Each root-to-leaf path in the tree represents a number.
• For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
• Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will
fit in 32-bit integer.
• A leaf node is a node with no children.
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
18
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};
return nullptr;
queue<TreeNode*> q;
q.push(root);
int index = 1;
q.pop();
index++;
q.push(current->right);
index++;
return root;
currentSum * 10 + root->val;
// If it's a leaf node, add the current sum to the total sum
20
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (root->left == nullptr && root->right == nullptr) {
totalSum += currentSum;
} else {
int totalSum = 0;
calculateRootToLeafSum(root, 0, totalSum);
return totalSum;
int main() {
string input; cout << "Enter the binary tree nodes in level order (use 'null' for empty nodes,
stringstream ss(input);
vector<string> nodes;
21
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
string node; while (ss
>> node) {
nodes.push_back(node);
return 0;
4. Output:
Problem: 8
1. Aim: Populating Next Right Pointers in Each Node.
2. Objective: Given a binary tree
22
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node,
the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
23
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Create left child
if (index < nodes.size() && nodes[index] != "null") {
current->left = new Node(stoi(nodes[index])); q.push(current-
>left);
}
index++;
return root;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
Node* prev = nullptr;
24
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (prev != nullptr) {
prev->next = nullptr;
}
}
}
queue<Node*> q;
q.push(root); cout << "[";
while (!q.empty()) {
Node* current = q.front();
q.pop(); if (current) {
cout << current->val;
if (current->next) {
cout << " -> " << current->next->val;
} else {
cout << " -> #"; // Indicate the end of the level
}
cout << ", ";
if (current->left) q.push(current->left);
if (current->right) q.push(current->right);
}
}
cout << "\b\b]"; // Remove the last comma and space
}
int main() {
string input;
cout << "Enter
the binary tree
nodes in level
order (use 'null'
for empty nodes,
space-separated):
"; getline(cin,
input);
25
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Split the input string into a vector
stringstream ss(input);
vector<string> nodes; string node;
while (ss >> node) {
nodes.push_back(node);
}
return 0;
}
4. Output:
Problem: 9
1. Aim: Count Paths That Can Form a Palindrome in a Tree.
2. Objective: You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted
at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-
indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the
root, parent[0] == -1.
You are also given a string s of length n, where s[i] is the character assigned to the edge
between i and parent[i]. s[0] can be ignored.
Return the number of pairs of nodes (u, v) such that u < v and the characters assigned to
edges on the path from u to v can be rearranged to form a palindrome.
A string is a palindrome when it reads the same backwards as forwards.
26
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Code:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
return count;
}
private:
void dfs(int node, int mask, vector<vector<int>>& tree, string& s, unordered_map<int, int>&
freqMap, int& count) {
// Update the mask for the current character
mask ^= (1 << (s[node] - 'a'));
27
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Increment the frequency of the current mask
freqMap[mask]++;
// Decrement the frequency of the current mask after returning from DFS
freqMap[mask]--;
}
};
int main() {
int n;
cout << "Enter the number of nodes: ";
cin >> n;
string s;
cout << "Enter the string s: ";
cin >> s;
Solution solution;
int result = solution.countPairs(parent, s);
cout << "The number of valid pairs is: " << result << endl;
return 0;
}
4. Output:
28
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
29