Topic 4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.
方法1:With Links to Parents: trace p and q’s paths up until they intersect. This would require a) being able to mark nodes as isVisited or b) being able to store some data in an additional data structure, such as a hash table.
方法2:Follow a chain if p and q are on the same side. Else, found the first common ancestor.
Time is O(n) on a balanced tree. 【This is because covers is called on 2n nodes in the first call, after that, branches left or right, at which point covers will be called on n, then n/2. This results in a runtime of O(n)??】. We cannot do better because we need to potentially look at every node in the tree. But we may be able to improve it by a constant multiple.
方法3:Each sbutree is searched over and over again. We should only need to search the entire tree once to find p and q, then bubble up the findings to earlier nodes in the stack.
----Returns p, if root’s subtree includes p (and not q)
----Returns q, if root’s subtree includes q (and no p)
----Returns null, if neither p nor q are in root’s subtree.
----Else, returns the common ancestor of q and p.
----Final case: commonAncestor(n.left) & commonAncestor(n.right) both return non-null value.
方法4:First search through the entrie tree to make sure both nodes exist, then use a method just return TreeNode. 【这里方法3和方法4需要时间再仔细看一下,问到了再看】
public class c4_7 {
// Checks how many “special” nodes are located under this root
public static int covers(TreeNode root, TreeNode p, TreeNode q) {
int num=0;
if (root == null) return num;
if (root == p || root == q) num+= 1;
num+=covers(root.left,p,q);
if(num==2) // found p and q,这部分要不要问题都不大,可以减少点时间
return num;
return num+covers(root.right,p,q);
}
public static TreeNode commonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
if (q == p) return p;
if (root==q||root==p) return root;//这一句必须有,因为底下的covers没有考虑头结点,有可能一个节点在左边,一个为头,nodesFromLeft=1
int nodesFromLeft = covers(root.left, p, q); // Check left side
if (nodesFromLeft == 2) {//如果两个都在左边,返回root.left,依次递归
if(root.left == p || root.left == q) return root.left;
else return commonAncestor1(root.left, p, q);
}
int nodesFromRight = covers(root.right, p, q);// Check right side
if(nodesFromRight == 2) {
if(root.right == p || root.right == q) return root.right;
else return commonAncestor1(root.right, p, q);
}
else return root;//剩下的只有一种情况,就是一边一个,所以判断条件可以不写
}
public static class Result {
public TreeNode node;
public boolean isAncestor;
public Result(TreeNode n, boolean isAncestor) {
node = n;
this.isAncestor = isAncestor;
}
}
public static Result commonAncestorHelper(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return new Result(null, false);
}
if (root == p && root == q) {// q==p的情况
return new Result(root, true);
}
Result rx = commonAncestorHelper(root.left, p, q);
if (rx.isAncestor) {
return rx;
}
Result ry = commonAncestorHelper(root.right, p, q);
if (ry.isAncestor) { // Found common ancestor
return ry;
}
if (rx.node != null && ry.node != null) {//1.左右都有返回值,说明左右都找到了,就是它了
return new Result(root, true);
} else if (root == p || root == q) {//就在这一点上
boolean isAncestor = rx.node != null || ry.node != null ? true : false;
return new Result(root, isAncestor);//在这一点上,如果左边或者右边有返回值,就是它了;没有就不是它
} else {
return new Result(rx.node != null ? rx.node : ry.node, false);//2.左边或右边有返回值,就一个,返回这个返回值; 3. ..
//三种情况:1. 左右都有返回值,就是它;2. 左边或右边有返回值,就一个,返回这个返回值; 3. 左右都没返回值,返回Null
}
}
public static TreeNode commonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
Result r = commonAncestorHelper(root, p, q);
if (r.isAncestor) {
return r.node;
}
return null;
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TreeNode root = TreeNode.createMinimalBST(array);
TreeNode p = root.find(6);
TreeNode q = root.find(10);
TreeNode ancestor = commonAncestor2(root, p, q);
System.out.println(ancestor.data);
}
}
//结果
8