## Saturday, March 19, 2016

### Find palindrome in Tree | allenlipeng47

Find palindrome in Tree | allenlipeng47
This is an application for rolling hash queue. In the picture below, let's find the palindrome in binary tree. So, the right answer should be 1-2-3-2-1.

In order to do that, I recursively visit each node. And I maintain 2 rolling hash queues during recursion. When it goes to a leaf node, check the rolling hash of two queues first. If the rolling hash are the same, then check if the queue elements are the same. By rolling hash, it saves a lot of comparison.

```    static boolean findPalindromeInTree(Tree tree){
if(tree == null){
return false;
}
TpData data = new TpData();
data.depth++;
if(tree.left!=null && findPalindromeInTreeUtil(tree.left, data)){
return true;
}
if(tree.right !=null && findPalindromeInTreeUtil(tree.right, data)){
return true;
}
return false;
}

static boolean findPalindromeInTreeUtil(Tree tree, TpData data){
/* Modify first queue and second queue depending on odd/even of depth */
data.depth++;
if(data.depth % 2 == 1){
}
else{
data.q2.removeLast();
}
/* Check q1 and q2 by rolling hash */
if(tree.left==null && tree.right==null && data.depth > 1 && data.q1.equals(data.q2)){
System.out.println(data.q1);
return  true;
}
/* Find palindrome in left, right tree */
if(tree.left!=null && findPalindromeInTreeUtil(tree.left, data)){
return true;
}
if(tree.right !=null && findPalindromeInTreeUtil(tree.right, data)){
return true;
}
/* No palindrome found in both left, right, backtrace */
if(data.depth % 2 == 1) {
data.q1.removeLast();
data.q2.removeFirst();
}
else {
data.q2.removeFirst();
}
data.depth--;
return false;
}

/** TreePalindromeData**/
static class TpData {
RollingHashQueue q1;
RollingHashQueue q2;
int depth;
public TpData(){
q1 = new RollingHashQueue();
q2 = new RollingHashQueue();
depth = 0;
}
}

public static void main(String[] args) {
Tree t1 = new Tree(1);
Tree t2 = new Tree(6);
Tree t3 = new Tree(4);
Tree t4 = new Tree(2);
Tree t5 = new Tree(3);
Tree t6 = new Tree(2);
Tree t7 = new Tree(5);
Tree t8 = new Tree(1);
t1.left = t2; t1.right = t4;
t2.right = t3;
t4.right = t5;
t5.right = t6;
t6.left = t7; t6.right = t8;
findPalindromeInTree(t1);
}```