一、题目内容
二、题解
解法1:
思路
代码
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);//p、q都不存在于当前子树if (left == null && right == null) {return null;}//p、q存在于右子树中if (left == null) {return right;}//p、q存在于左子树中if (right == null) {return left;}//p、q在root左右两侧,root为最近公共祖先return root;}}
