Extract Leaves of a Binary Tree in a Doubly Linked List : GeeksforGeeks

Problem link: http://www.geeksforgeeks.org/connect-leaves-doubly-linked-list/

Problem Statement:
Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In DLL, left means previous pointer and right means next pointer.

Solution Idea:
The first step is to be able to print the leaves. It is simple, you can do a in-order traversal and put a condition (isLeaf) before printing. When you understand this, the rest is making the program work with the boundary conditions. To trim the original tree, we need the reference to the parent pointer in the recursive call.

Solution Gist:


public class BTLeavesToDLL {
public static TNode<Integer> getLeaves(TNode<Integer> root){
if(root==null){
return null;
}
if(isLeaf(root)){
return root;
}
else{
TNode<Integer> left=getLeaves(root.left);
if(left!=null){
TNode<Integer> temp=left;
while(temp.right!=null) temp=temp.right;
temp.right=getLeaves(root.right);
if(temp.right != null){//boundary
temp.right.left=temp;
}
return left;
}
else{//boundary
return getLeaves(root.right);
}
}
}
public static TNode<Integer> getLeavesAndTrimOriginal(TNode<Integer> root){
return getLeavesAndTrimOriginalRec(root,null,false);
}
private static TNode<Integer> getLeavesAndTrimOriginalRec(TNode<Integer> root,TNode<Integer> parent,boolean isLeftChild){
if(root==null){
return null;
}
if(isLeaf(root)){
if(parent!=null){
if(isLeftChild) parent.left=null;
else parent.right=null;
}
return root;
}
else{
TNode<Integer> left=getLeavesAndTrimOriginalRec(root.left,root,true);
if(left!=null){
TNode<Integer> temp=left;
while(temp.right!=null) temp=temp.right;
temp.right=getLeavesAndTrimOriginalRec(root.right,root,false);
if(temp.right != null){//boundary
temp.right.left=temp;
}
return left;
}
else{//boundary
return getLeavesAndTrimOriginalRec(root.right,root,false);
}
}
}
private static boolean isLeaf(TNode<Integer> root){
if(root==null) return false; //if it is not a node it is not a leaf
else return root.left == null && root.right==null;
}
}


public class BTLeavesToDLLTest {
@Test
public void testGetLeaves() {
TNode<Integer> r= new TNode<Integer>(20);
r.left= new TNode<Integer>(8);
r.left.left= new TNode<Integer>(4);
r.left.right= new TNode<Integer>(12);
r.left.right.left= new TNode<Integer>(10);
r.left.right.right= new TNode<Integer>(14);
r.right= new TNode<Integer>(22);
r.right.right= new TNode<Integer>(25);
TNode<Integer> leaves=BTLeavesToDLL.getLeaves(r);
assertEquals(new Integer(4), leaves.data);
assertEquals(new Integer(10), leaves.right.data);
assertEquals(new Integer(14), leaves.right.right.data);
assertEquals(new Integer(25), leaves.right.right.right.data);
assertEquals(new Integer(14), leaves.right.right.right.left.data);
assertEquals(new Integer(10), leaves.right.right.right.left.left.data);
assertEquals(new Integer(4), leaves.right.right.right.left.left.left.data);
}
@Test
public void testGetLeavesAndTrimOriginal() {
TNode<Integer> r= new TNode<Integer>(20);
r.left= new TNode<Integer>(8);
r.left.left= new TNode<Integer>(4);
r.left.right= new TNode<Integer>(12);
r.left.right.left= new TNode<Integer>(10);
r.left.right.right= new TNode<Integer>(14);
r.right= new TNode<Integer>(22);
r.right.right= new TNode<Integer>(25);
TNode<Integer> leaves=BTLeavesToDLL.getLeavesAndTrimOriginal(r);
assertEquals(new Integer(4), leaves.data);
assertEquals(new Integer(10), leaves.right.data);
assertEquals(new Integer(14), leaves.right.right.data);
assertEquals(new Integer(25), leaves.right.right.right.data);
assertEquals(new Integer(14), leaves.right.right.right.left.data);
assertEquals(new Integer(10), leaves.right.right.right.left.left.data);
assertEquals(new Integer(4), leaves.right.right.right.left.left.left.data);
assertEquals(new Integer(8), r.left.data);
assertEquals(new Integer(22), r.right.data);
assertEquals(new Integer(12), r.left.right.data);
assertNull(r.left.left);
assertNull(r.left.right.left);
assertNull(r.left.right.right);
assertNull(r.right.right);
}
}

Tagged: , , , , , , , , ,

Leave a comment