Category: PostOrder Traversal

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

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