Posts

Showing posts with the label Day18

Convert a Binary Tree into its Mirror Tree

Image
Mirror of a Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged. Code: package trees; public class N1804_MirrorTree {     Node root = null;     static void convertToMirror(Node node){         if(node == null){             return;         }         Node temp = node.left;         node.left = node.right;         node.right = temp;         convertToMirror(node.left);         convertToMirror(node.right);     }     static void printTree(Node node){         if(node == null)             return;        ...

Write Code to Determine if Two Trees are Identical

Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees. Code: package trees; class Node{     int data;     Node left, right;     Node(int data){         this.data = data;         right = left = null;     } } public class N1802_IdenticalTrees {     Node tree1 = null, tree2 = null;     boolean isIdentical(Node root1, Node root2){         if(root1 == null && root2 == null){             return true;         }         if(root1!= null && root2!= null){          ...

Program to count leaf nodes in a binary tree

Problem link Recursion Steps: If node is null return zero If left and right nodes are null return 1 Return count of leaves for left and right subtrees Code: public class CountLeaves {     Node root;         int countLeaves(Node node){                 if(node == null)             return 0;                 if(node.left == null && node.right == null)             return 1;                 return countLeaves(node.left)+countLeaves(node.right);     }         public static void main(String[] args){         CountLeaves tree = new CountLeaves();      ...

Program to delete a tree

Source: problem link Code: public class DeleteTree {     Node root;         void deleteTree(Node node){         if(node == null)             return;         deleteTree(node.left);         deleteTree(node.right);         System.out.println("node to delete "+node.data);         node = null;     }         void deleteTreeRef(Node root)     {         deleteTree(root);         root=null;     }         public static void main(String[] args){         DeleteTree tree = new DeleteTree();               ...

Convert a binary tree into its mirror tree

problem link Code: class Node{ int data; Node left, right; Node(int data){     this.data = data; } } public class MirrorTree {         Node root;         void converToMirror(Node node){                 if(node == null){             return;         }                 Node temp = node.right;         node.right = node.left;         node.left = temp;         converToMirror(node.left);         converToMirror(node.right);     }         void printTree(Node node){                 if(node == null)  ...

Write a progtam to calculate size of a tree

Recursion Steps:   Size of a tree= 1+size of left subtree + size of right subtree   Base case : return zero if  a node is null Code:     Time:O(n) class Node{     int data;     Node left, right;     Node(int data){         this.data = data;     } } public class SizeOfBinaryTree {       static int calculateSize(Node node){           if(node == null){               return 0;           }                     return (1+calculateSize(node.left)+calculateSize(node.right));       }             public static void main(String[] args){      ...

Write code to determine if two trees are identical

Steps: If two nodes are null we reached the end for that subtree and we will return true If two nodes are not null we check for data and check recursively their left and right sub trees If one node is null and other is not null then we return false which is the last statement in the function.     Code: class Node{     int data;     Node left, right;         Node(int data){         this.data = data;     } } public class IdenticalTrees {      Node tree1, tree2;           boolean isIdentical(Node n1, Node n2){          if(n1 == null && n2 == null){              return true;          }          if(n1!= null && n2!=null){   ...