我遇到了以下的实现方式,花了一些时间,但仍然无法掌握其思想。请问是否有人能够逐行解释它在做什么?我不明白在何时可以确定一个节点是祖先。
谢谢
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left != null && right != null) return root;
return left != null ? left : right;
}
}