Category: Preorder Traversal

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

Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two

import java.lang.Math;import java.io.*;  class GFG {      static class Node {        Node left, right;        int val;        public Node(int val)        {            this.val = val;            this.left = this.right = null;        }    }                  public static int minAbsDiff(Node root)    {                          int[] minDiff = new int[1];          minDiff[0] = Integer.MAX_VALUE;                                          postOrder(root);                                  preOrder(root, minDiff);          return minDiff[0];    }      public static int postOrder(Node root)    {        if (root == null)            return 0;        root.val += postOrder(root.left)                    + postOrder(root.right);        return root.val;    }      public static void preOrder(Node root,                                int[] minDiff)    {        if (root == null)            return;                          int leftDiff            = Math.abs(root.left.val                       – (root.val – […]

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

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

Lexicographically smallest Palindromic Path in a Binary Tree

   #include using namespace std;    struct Node {     char data;     Node *left, *right; };    Node* newNode(char data) {     Node* temp = new Node();     temp->data = data;     temp->left = temp->right = NULL;     return temp; }    bool checkPalindrome(string s) {     int low = 0, high = (int)s.size() – 1;        while (low < high) […]

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