Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/com/algorithm/question5/FindSameAncestor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.algorithm.question5;

public class FindSameAncestor {
public Node buildBinaryTree(int... datas) {
if(datas.length < 7) {
return null;
}

Node head = new Node(datas[0]);
head.left = new Node(datas[1]);
head.right = new Node(datas[2]);
head.left.left = new Node(datas[3]);
head.left.right = new Node(datas[4]);
head.right.left = new Node(datas[5]);
head.right.right = new Node(datas[6]);

return head;
}

public Node findSameAncestorInBinaryTree(Node head, Node node1, Node node2) {
if(head == null) {
return null;
}

boolean left = isNodeInTree(head.left, node1, node2);
boolean right = isNodeInTree(head.right, node1, node2);

if(left && right) {
return head;
} else if(left && !right) {
return findSameAncestorInBinaryTree(head.left, node1, node2);
} else if(!left && right) {
return findSameAncestorInBinaryTree(head.right, node1, node2);
}

return null;
}

private boolean isNodeInTree(Node head, Node node1, Node node2) {
if(head == null) {
return false;
}

if(head.data == node1.data || head.data == node2.data) {
return true;
}

return isNodeInTree(head.left, node1, node2) || isNodeInTree(head.right, node1, node2);
}

public Node findSameAncestorInSearchTree(Node head, Node node1, Node node2) {
if(head == null) {
return null;
}

int high = node1.data > node2.data ? node1.data : node2.data;
int low = node1.data == high ? node2.data : node1.data;

if(head.data > low && head.data < high) {
return head;
} else if(head.data >= high){
return findSameAncestorInSearchTree(head.left, node1, node2);
} else if(head.data <= low) {
return findSameAncestorInSearchTree(head.right, node1, node2);
}

return null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/algorithm/question5/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.algorithm.question5;

public class Node {
public int data;
public Node left;
public Node right;

public Node(int data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.algorithm.question5.mark;

import org.junit.Assert;
import org.junit.Test;

import com.algorithm.question5.FindSameAncestor;
import com.algorithm.question5.Node;

import static org.hamcrest.core.Is.is;

public class Test_findSameAncestor {
private FindSameAncestor findSameAncestor = new FindSameAncestor();

@Test
public void findSameAncestorInBinaryTree_head_is_null() {
// setup
Node head = null;

// execute
Node result = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5));

// verify
Assert.assertEquals(null, result);
}

@Test
public void findSameAncestorInBinaryTree_only_head_is() {
// setup
Node head = new Node(1);

// execute
Node result = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5));

// verify
Assert.assertEquals(null, result);
}

@Test
public void findSameAncestorInBinaryTree_example() {
// setup
Node head = findSameAncestor.buildBinaryTree(1,2,3,4,5,6,7);

// execute
Node result1 = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(5));
Node result2 = findSameAncestor.findSameAncestorInBinaryTree(head, new Node(4), new Node(6));

// verify
Assert.assertThat(result1.data, is(2));
Assert.assertThat(result2.data, is(1));
}

@Test
public void findSameAncestorInSearchTree_head_is_null() {
// setup
Node head = null;

// execute
Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(4), new Node(5));

// verify
Assert.assertEquals(null, result1);
}

@Test
public void findSameAncestorInSearchTree_only_head_is() {
// setup
Node head = new Node(1);

// execute
Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(4), new Node(5));

// verify
Assert.assertEquals(null, result1);
}

@Test
public void findSameAncestorInSearchTree_example() {
// setup
Node head = findSameAncestor.buildBinaryTree(4,2,6,1,3,5,7);

// execute
Node result1 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(1), new Node(3));
Node result2 = findSameAncestor.findSameAncestorInSearchTree(head, new Node(1), new Node(7));

// verify
Assert.assertThat(result1.data, is(2));
Assert.assertThat(result2.data, is(4));
}

}