Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@anil477
Created July 8, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/1b355c54204bcc0ce9e4978e7ba6f745 to your computer and use it in GitHub Desktop.
Save anil477/1b355c54204bcc0ce9e4978e7ba6f745 to your computer and use it in GitHub Desktop.
Given a binary tree,remove all the half nodes
// http://www.geeksforgeeks.org/given-a-binary-tree-how-do-you-remove-all-the-half-nodes/
class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
void printInorder(Node node)
{
if (node != null)
{
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
}
// Removes all nodes with only one child and returns
// new root (note that root may change)
Node RemoveHalfNodes(Node node)
{
if (node == null)
return null;
node.left = RemoveHalfNodes(node.left);
node.right = RemoveHalfNodes(node.right);
if (node.left == null && node.right == null)
return node;
/* if current nodes is a half node with left
child NULL left, then it's right child is
returned and replaces it in the given tree */
if (node.left == null)
{
return node.right;
}
/* if current nodes is a half node with right
child NULL right, then it's right child is
returned and replaces it in the given tree */
if (node.right == null)
{
return node.left;
}
return node;
}
// Driver program
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
Node NewRoot = null;
tree.root = new Node(2);
tree.root.left = new Node(7);
tree.root.right = new Node(5);
tree.root.left.right = new Node(6);
tree.root.left.right.left = new Node(1);
tree.root.left.right.right = new Node(11);
tree.root.right.right = new Node(9);
tree.root.right.right.left = new Node(4);
System.out.println("the inorder traversal of tree is ");
tree.printInorder(tree.root);
NewRoot = tree.RemoveHalfNodes(tree.root);
System.out.print("\nInorder traversal of the modified tree \n");
tree.printInorder(NewRoot);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment