我试图从破解代码访谈的书中解决问题4.7(非常酷的书!)。
设计算法并编写代码以找到第一个共同的祖先 二叉树中的两个节点。避免在a中存储其他节点 数据结构。注意:这不一定是二叉搜索树。
我想出了这个与本书中提供的解决方案并不相符的解决方案。我想知道是否有人能找到任何瑕疵吗?
解决方案: 我创建了一个包装类来保存第一个共同的祖先(如果找到它)和2个布尔值来跟踪在搜索树时是否找到了a或b。请在下面的代码中阅读添加的评论。
public static void main (String args[]){
NodeTree a, b, head, result; //initialise and fill with data
fillTreeTestData(head);
pickRandomNode(a);
pickRandomNode(b);
result = commonAnsestor(a,b,head);
if(result != null)
System.out.println("First common ansestor "+result);
else
System.out.println("Not found");
}
class TreeNode{
Object value;
TreeNode right, left;
}
class WraperNodeTree{
boolean found_a;
boolean found_b;
NodeTree n;
WraperNodeTree (boolean a, boolean b, NodeTree n){
this.n = n;
this.a = a;
this.b = b;
}
}
static WraperNodeTree commonAnsestor(NodeTree a, NodeTree b, NodeTree current){
// Let's prepare a wraper object
WraperNodeTree wraper = new WraperNodeTree(false, false, null);
// we reached the end
if(current == null) return wraper;
// let's check if current node is either a or b
if(a != null)
wraper.found_a = current.value.equals(a.value);
else if(b != null)
wraper.found_b = current.value.equals(b.value);
else
return wraper; // if both are null we don't need to keep searching recoursively
// if either a or b was found let's stop searching for it for performance
NodeTree to_search_a = wraper.found_a ? null : a;
NodeTree to_search_b = wraper.found_b ? null : b;
// let's search the left
WraperNodeTree wraperLeft = common(to_search_a,to_search_b,current.left);
// if we already have a common ancester just pass it back recoursively
if(wraperLeft.n != null) return wraperLeft;
WraperNodeTree wraperRight = common(to_search_a,to_search_b,current.right);
if(wraperRight.n != null)return wraperRight;
// keep the wraper up to date with what we found so far
wraper.a = wraper.found_a || wraperLeft.found_a || wraperRight.found_a;
wraper.b = wraper.found_b || wraperLeft.found_b || wraperRight.found_b;
// if both a and b were found, let's pass the current node as solution
if(wraper.found_a && wraper.found_b)
wraper.n = current;
return wraper;
}
答案 0 :(得分:2)
如果是关于发现瑕疵的话:
缺陷#1: 我认为你的代码中有太多的拼写错误,这可能会使访问者在第一次阅读代码时感到困惑(而且你不想要这样!)。例如,你写了NodeTree'和' TreeNode'互换。此外,您定义了' commonAncestor()'然后打电话给#com; common()'。这些事情让面试官感到困惑,使他偏离了重要的事情,这就是理解你解决问题的方法。
缺陷#2:除了错别字,我认为另一个缺陷是这段代码难以理解。我认为原因之一是因为你已经回归'语句遍及函数体(在开头,中间和结尾)。这通常应该'避免支持可读性。
通常我的方法是按以下方式组织代码:
但是当你在中间有回复语句时,它会让读者更难想象这个流程。
缺陷#3 :我认为您正在尝试使用相同的功能解决两个问题(commonAncestor)。您正试图同时搜索' a'和' b'并跟踪共同的祖先。我认为如果这是一个面试问题,你可以将这两个目标分开,而不是简单。
例如,考虑一下这段代码(可能不完美,需要一些额外的边界检查):
/**
* [Assumption]: If we call firstCommonAncestor(a, b, root)
* we TRUST that root contains both a and b.
*
* You can (and should) discuss this
* assumption with your interviewer.
*/
public static Node firstCommonAncestor(Node a, Node b, Node root) {
// If root matches any of the nodes (a or b),
// then root is the first common ancestor
// (because of our assumption)
if(root == a || root == b) return root;
// Search for a and b in both sides
SearchResult leftResult = searchNodes(a, b, root.left);
SearchResult rightResult = searchNodes(a, b, root.right);
// If a and b are on the same side (left or right), then we
// call firstCommonAncestor on that side and that’s it
if(leftResult.aFound && leftResult.bFound)
return firstCommonAncestor(a, b, root.left);
else if(rightResult.aFound && rightResult.bFound)
return firstCommonAncestor(a, b, root.right);
else {
// If a and b are in different sides,
// then we just found the first common ancestor
return root;
}
}
class SearchResult {
boolean aFound, bFound;
}
在上面的代码中,我将实际搜索' a'和' b'在一个名为searchNodes的不同函数中,如果你的面试官要求它,这个函数相当容易实现。但他甚至可能不这样做。如果他这样做,那么他已经理解了你的方法,所以现在更容易让代码变得更复杂了#34;没有混淆面试官。
我希望这会有所帮助。