Sponsor

test

Sample Text

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

About & Social

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum viverra pharetra. Nulla facilisis, sapien non pharetra venenatis, tortor erat tempus est, sed accumsan odio ante ac elit. Nulla hendrerit a est vel ornare. Proin eu sapien a sapien dignissim feugiat non eget turpis. Proin at accumsan risus. Pellentesque nunc diam, congue ac lacus

My First Blog Page blog.codingninjas.in Coding Ninja first Blog ...

Search This Blog

Archive

Tags

Post Top Ad

ads

Latest Admit Cards

Beauty

Latest Admissions

Hot

Latest Syllabus

Latest Answer Key

About Us

Recent

Subscribe To Get All The Latest Updates!

email updates

Recent Posts

ads

Post Top Ad

Remove Leaf Node Of Binary Tree

No comments :



Problem: Given a binary tree, how do you remove leaves of it?
Solution: By using post-order traversal we can solve this problem (other traversals would also work).
Examples:
Input : 20 10 5 15 30 25 35
Output : Inorder before Deleting the leaf node
         5 10 15 20 25 30 35
         Inorder after Deleting the leaf node
         10 20 30

        This is the binary search tree where we
        want to delete the leaf node.
              20
           /     \
          10      30
         /  \    /  \
       5     15 25   35 

      After deleting the leaf node the binary 
      search tree looks like
              20
           /     \
          10      30
     
public static BinaryTreeNode<Integer> removeLeaf(BinaryTreeNode<Integer> root)
 {
  if(root == null)
  {
   return null;
  }
  
  if(root.left==null && root.right==null)
  {
   return null;
  }
  
  
  BinaryTreeNode<Integer> left = removeLeaf(root.left);
  
  BinaryTreeNode<Integer> right = removeLeaf(root.right);
  
  
  root.left  = left;
  root.right = right;
  
  return root;
 }

No comments :

Post a Comment