Category: Inorder Traversal

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

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

Modify Binary Tree by replacing each node with the product of all remaining nodes

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

Continue Reading

Median of all nodes from a given range in a Binary Search Tree ( BST )

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

Continue Reading

Tutorial on Binary Tree

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

Preorder, Postorder and Inorder Traversal of a Binary Tree using a single Stack

   #include using namespace std;    struct Node {     int data;     struct Node *left, *right;        Node(int data)     {         this->data = data;         left = right = NULL;     } };    void allTraversal(Node* root) {          vector pre;             vector post;             vector in;                  stack s;                  s.push(make_pair(root, 1)); […]

Continue Reading