Category: Tree

Length of Longest Increasing Subsequences (LIS) using Segment Tree

#include using namespace std;  #define M 100000  vector tree(4 * M + 1);  void update_tree(int start, int end,                 int update_idx, int length_t,                 int count_c, int idx){            if (start == end        && start == update_idx) {        tree[idx].first            = max(tree[idx].first, length_t);        tree[idx].second = count_c;        return;    }          if (update_idx < start        || end < update_idx) {        return;    }          int mid = (start + end) / 2;      update_tree(start, mid, update_idx,                length_t, count_c,                2 * idx);    update_tree(mid + 1, end, update_idx,                length_t, […]

Continue Reading

Convert given Binary Tree to Symmetric Tree by adding minimum number of nodes

  #include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = right = NULL;    }};  Node* buidSymmetericTree(Node* root1,                         Node* root2){        if (root1 == NULL and root2 == NULL) {        return NULL;    }          if (root1 == NULL) {                          Node* node = new Node(root2->val);        root1 = node;    }          if (root2 == NULL) {                          Node* node = new Node(root1->val);        root2 = node;    }                  if (root1->val != root2->val) {        int temp = root1->val […]

Continue Reading

Check if all elements of given Linked List corresponds to a downward path from any node in given Binary Tree

  #include “bits/stdc++.h”using namespace std;  struct listNode {    int val;    struct listNode* next;          listNode(int data)    {        this->val = data;        next = NULL;    }};  struct treeNode {    int val;    treeNode* right;    treeNode* left;          treeNode(int data)    {        this->val = data;        this->left = NULL;        this->right = NULL;    }};  bool isPathUtil(listNode* curList,                treeNode* curTree){            if (curList == NULL)        return true;          if (curTree == NULL)        return false;      if (curList->val == curTree->val) {                          return isPathUtil(curList->next,                          curTree->left)               || isPathUtil(curList->next,                             curTree->right);    }    else {                  return false;    }}  bool isPath(listNode* head, treeNode* root){              if (root == NULL)        return false;              if (head == […]

Continue Reading

Find direction of path followed from root by a linked list in a Binary Tree

#include using namespace std;  struct ListNode {    int data;    ListNode* next;    ListNode(int data)    {        this->data = data;        this->next = NULL;    }};  struct TreeNode {    TreeNode* left;    TreeNode* right;    int val;      TreeNode(int x)        : left(NULL), right(NULL), val(x)    {    }};  ListNode* makeList(int arr[], int n){    ListNode* h = NULL;    ListNode* root;    for (int i = 0; i < n; i++) {        int data = arr[i];        ListNode* node = new ListNode(data);          if (h == NULL) {            h = node;            root = h;        }        else {            root->next […]

Continue Reading

Level order traversal by converting N-ary Tree into adjacency list representation with K as root node

#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

Flatten a Binary Search Tree to convert the tree into a wave list in place only

  #include using namespace std;  struct TreeNode {      int data;    TreeNode* right;    TreeNode* left;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  TreeNode* insertNode(int data,                     TreeNode* root){    if (root == NULL) {        TreeNode* node            = new TreeNode(data);        return node;    }    else if (data > root->data) {        root->right = insertNode(            data, root->right);    }    else if (data data) {        root->left = insertNode(            data, root->left);    }          return root;}  int countNodes(TreeNode* root){    if (root == NULL)        return 0;      else        return countNodes(root->left)               + countNodes(root->right) + 1;}  TreeNode* toWaveList(TreeNode* root){    int […]

Continue Reading

Maximize difference between pair of nodes in a given rooted tree such that one node is ancestor of another

#include using namespace std;  vector tree;vector visited;vector ancestorNum;  int maxDiff = INT_MIN;  void dfs(int src, int val, vector& W){          visited[src] = true;          for (auto neighbour : tree[src]) {                                  if (!visited[neighbour]            && (ancestorNum[neighbour]                > ancestorNum[src])) {                          maxDiff = max(                val – W[neighbour – 1],                maxDiff);                          dfs(neighbour, val, W);        }    }}  void bfs(int src, int N){            visited.assign(N, false);          queue q;              ancestorNum[src] = 0;          visited[src] = true;          q.push(src);          while (!q.empty()) {                  int cur = q.front();        q.pop();                  for (auto neighbour : tree[cur]) {                                      if […]

Continue Reading

Minimize sum of node values by filling given empty Tree such that each node is GCD of its children

#include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = NULL;        right = NULL;    }};  class Tree {public:    unordered_map depth;              int findDepth(Node* cur)    {        int mx = 0;        if (cur->left) {            mx = findDepth(cur->left);        }        if (cur->right) {            mx = max(mx, findDepth(cur->right));        }                          return depth[cur] = mx + 1;    }              int dfs(Node* cur, bool flag, int parValue)    {        if (parValue != -1) {            if (flag)                cur->val = parValue;            else                cur->val = parValue * 2;        }        int […]

Continue Reading

Minimum distance between two given nodes in an N-ary tree

#include using namespace std;  struct Node {    int val;    vector child;};  Node* newNode(int key){    Node* temp = new Node;    temp->val = key;    return temp;}  bool flag;  void findPath(Node* root, int key,              vector& arr){    if (!root)        return;    arr.push_back(root->val);        if (root->val == key) {        flag = 1;        return;    }        for (int i = 0; i < root->child.size(); i++) {          findPath(root->child[i], key, arr);                  if (flag == 1)            return;    }      arr.pop_back();    return;}  void findMinDist(Node* root, int A, int B){    if (root == NULL)        return;    int val = […]

Continue Reading