登录
  • #刷题

Lo‌‌‌‍‌‌‍‌‍‌‌‍‌‌‌‌‌‌‌‌‍‍‍‌‌‍‌‌‌‍‍‌west Common Ancestor of a Binary Tree 疑问

lvbxr
1400
3
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;

TreeNode l = lowestCommonAncestor(root.left, p, q);

TreeNode r = lowestCommonAncestor(root.right, p, q);

if(l != null && r != null)

{

return root;

}

return l != null ? l : r;

}

为什么过不了 oj ?
3条回复
热度排序

发表回复