/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private TreeNode ans;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
dfs(root,p,q);
return ans;
}
private boolean dfs(TreeNode root, TreeNode p, TreeNode q){
if(root == null) return false;
boolean lson = dfs(root.left,p,q);
boolean rson = dfs(root.right,p,q);
if((lson && rson) || (root.val == p.val || root.val == q.val) &&(lson || rson)){
ans = root;
}
return rson || lson || root.val == p.val || root.val == q.val;
}
}
后序遍历,找到的第一个root一定是最近公共祖先。