Category: Tree Traversals

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

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

Print all the paths from root to leaf, with a specified sum in Binary tree

  import java.util.*;  class GFG {      static List result        = new ArrayList();          static class Node {        int data;        Node left, right;    };          static Node newNode(int data)    {        Node temp = new Node();        temp.data = data;        temp.left = temp.right = null;        return temp;    }              static void pathSumUtil(        Node node, int targetSum,        Stack stack)    {        if (node == null) {            return;        }        stack.push(node.data);          if (node.left == null            && node.right == null) {            if (node.data == targetSum) {                result.add(new ArrayList(stack));            }        }        pathSumUtil(node.left,                    targetSum – node.data,                    stack);        pathSumUtil(node.right,                    targetSum – node.data,                    stack);        stack.pop();    }              static List […]

Continue Reading

Queries to find the sum of weights of all nodes with vertical width from given range in a Binary Tree

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

Continue Reading

Largest element in an N-ary Tree

Largest element in an N-ary TreeGiven an N-ary tree consisting of N nodes, the task is to find the node having the largest value in the given N-ary Tree.Examples:Input: Output: 90Explanation: The node with the largest value in the tree is 90.Input: Output: 95Explanation: The node with the largest value in the tree is 95.Approach: […]

Continue Reading

Sum of nodes in a Binary Search Tree with values from a given range

#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 Reading

Construct a Tree whose sum of nodes of any path is not divisible by the count of nodes in that path

   #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 Reading

Count of subtrees possible from an N-ary Tree

   #include #define MAX 300004 using namespace std;    vector graph[MAX]; int mod = 1e9 + 7;    int ans = 0;    int countSubtreesUtil(int cur, int par) {               int res = 1;             for (int i = 0;          i < graph[cur].size(); i++) {                     int v = graph[cur][i];            if […]

Continue Reading

Program to calculate Height and Depth of a node in a Binary Tree

#include using namespace std; struct Node {     int data;     Node *left, *right; }; Node* newNode(int item) {     Node* temp = new Node;     temp->data = item;     temp->left = temp->right = NULL;     return temp; } int findDepth(Node* root, int x) {          if (root == NULL)         return -1;          int dist = -1;          if ((root->data == […]

Continue Reading