展开全部
||public class Path {
public static List minPath(Node root, Node a, Node b) {
List result = new ArrayList();
if (root == null ||e69da5e6ba903231313335323631343130323136353331333361306461 a == null || b == null) {
return result;
}
List aPath = path(root, a);
List bPath = path(root, b);
int lastSameNodeIndex = getLastSameNodeIndex(aPath, bPath);
if (lastSameNodeIndex == -1) {
return result;
}
for (int i = aPath.size() - 1; i >= lastSameNodeIndex; i--) {
result.add(aPath.get(i));
}
for (int i = lastSameNodeIndex + 1; i
result.add(bPath.get(i));
}
return result;
}
private static int getLastSameNodeIndex(List aPath, List bPath) {
int i = -1;
while (i + 1
&& aPath.get(i + 1) == bPath.get(i + 1)) {
i++;
}
return i;
}
private static List path(Node root, Node n) {
List result = new ArrayList();
result.add(root);
if (root == n) {
return result;
}
if (root.left != null) {
List left = path(root.left, n);
if (left.size() > 0) {
result.addAll(left);
return result;
}
}
if (root.right != null) {
List right = path(root.right, n);
if (right.size() > 0) {
result.addAll(right);
return result;
}
}
result.clear();
return result;
}
}public class Node {
public char value;
public Node left;
public Node right;
public Node(char value) {
this.value = value;
}
}