#include using namespace std; struct Node { int data; vector child;}; Node* newNode(int key){ Node* temp = new Node; temp->data = key; return temp;} vector adj; void DFS(struct Node* node){ for (auto x : node->child) { if (x != NULL) { adj[node->data].push_back(x->data); adj[x->data].push_back(node->data); DFS(x); } }} void levelOrderTrav(int s, int N){ queue q; vector visited(N); q.push(s); q.push(-1); visited[s] = true; while (!q.empty()) { int v = q.front(); q.pop(); if (v == -1) { if (!q.empty()) q.push(-1); cout child).push_back(newNode(4)); (root->child).push_back(newNode(5)); (root->child[0]->child).push_back(newNode(6)); (root->child[0]->child).push_back(newNode(7)); (root->child[2]->child).push_back(newNode(8)); (root->child[3]->child).push_back(newNode(9)); (root->child[3]->child).push_back(newNode(10)); (root->child[3]->child).push_back(newNode(11)); int N = 11; int K = 5; adj.resize(N […]

Continue Reading#include using namespace std;class Node {public: int val; Node *left, *right;};Node* newNode(int item){ Node* temp = new Node(); temp->val = item; temp->left = temp->right = NULL; return temp;}int sum = 0;int rangeSumBST(Node* root, int low, int high){ if (root == NULL) return 0; queue q; q.push(root); while (q.empty() == false) { Node* curr = q.front(); q.pop(); if (curr->val >= low && curr->val val; } if (curr->left != NULL && curr->val > low) q.push(curr->left); if (curr->right != […]

Continue ReadingThe tree is a hierarchical Data Structure. A binary tree is a tree that has at most two children. The node which is on the left of the Binary Tree is called “Left-Child” and the node which is the right is called “Right-Child”. Also, the smaller tree or the subtree in the left of the […]

Continue Readingimport math class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def nearestPow(x, base): k = int(math.log(x, base)) if abs(base**k – x) < abs(base**(k+1) - x): return base**k else: return base**(k+1) def printLevelOrder(root): if root is None: return q = [] q.append(root) while q: count = len(q) while count > 0: temp = q.pop(0) print(temp.val, end=’ ‘) if temp.left: q.append(temp.left) if temp.right: q.append(temp.right) count -= 1 def replaceNodes(root): que = [root] lvl = 1 minPrev = root.val minCurr = […]

Continue Reading#include using namespace std; void assignValues(int Edges[][2], int n) { vector tree[n + 1]; for (int i = 0; i < n - 1; i++) { int u = Edges[i][0]; int v = Edges[i][1]; tree[u].push_back(v); tree[v].push_back(u); } vector visited(n + 1, false); vector answer(n […]

Continue ReadingSum of Bitwise AND of the sum of all leaf and non-leaf nodes for each level of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to find the sum of Bitwise AND of the sum of all leaf nodes and the sum of all non-leaf nodes for each level […]

Continue Reading#include using namespace std; struct node { int data; struct node *left, *right; }; node* newNode(int key) { node* temp = new node; temp->data = key; temp->left = temp->right = NULL; return (temp); } void printLevelOrder(node* root) { if (!root) return; queue q; q.push(root); while […]

Continue Reading