题目
思路
逐层向下递归,直至找到公共祖先
代码块
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null)
{
return null;
}
if (root.val == p.val || root.val == q.val)
{
return root;
}
var left = LowestCommonAncestor(root.left, p, q);
var right = LowestCommonAncestor(root.right, p, q);
if (left == null)
{
return right;
}
if (right == null)
{
return left;
}
return root;
}
}
运行结果