Sunday, 28 Nov 2021
Category: tree-traversal

#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 […]

#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 == […]

#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 […]

#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 […]

#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 […]

#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 […]

#include using namespace std;  struct Node {    int data;    Node* left;    Node* right;};  Node* newNode(int d){    Node* n = new Node;    n->data = d;    n->left = NULL;    n->right = NULL;    return n;}  void findwt(Node* root, int wt[],            map& um,            int width = 0){        if (root == NULL) {        return;    }              um[width] += wt[root->data];          findwt(root->left, wt, um,           width – 1);          findwt(root->right, wt, um,           width + 1);}  void solveQueries(int wt[], Node* root,                  vector queries){            map um;          findwt(root, wt, um);              int x = 0;          for […]

#include using namespace std;  class Node {public:    int data;    Node *left, *right;          Node(int data)    {        this->data = data;        left = NULL;        right = NULL;    }};  Node* newNode(int value){    Node* temp = new Node(value);    return (temp);}  int findProduct(Node* root){        if (root == NULL)        return 1;              return (root->data            * findProduct(root->left)            * findProduct(root->right));}  void display(Node* root){        if (root == NULL)        return;              display(root->left);          cout data right);}  void convertTree(int product,                 Node* root){        if (root == NULL)        return;              root->data = product / (root->data);          convertTree(product, root->left);          convertTree(product, root->right);}  int main(){        Node* root = newNode(1);    root->left […]

#include using namespace std;  struct Node {    struct Node *left, *right;    int key;};  Node* newNode(int key){    Node* temp = new Node;    temp->key = key;    temp->left = temp->right = NULL;    return temp;}  Node* insertNode(Node* node, int key){            if (node == NULL)        return newNode(key);          if (key < node->key)        node->left = insertNode(            node->left, key);      else if (key > node->key)        node->right = insertNode(            node->right, key);          return node;}  void getIntermediateNodes(    Node* root, vector& interNodes,    int node1, int node2){        if (root == NULL)        return;          getIntermediateNodes(root->left,                         interNodes,                         node1, […]