public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (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 ? right : left;
}
宇智波爱编程
发布了225 篇原创文章 · 获赞 43 · 访问量 9万+
私信
关注
相关文章
- 03-28[LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点
- 03-28236.Lowest Common Ancestor of a Binary Tree
- 03-2830 Day Challenge Day 7 | Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
- 03-28SP14932 LCA - Lowest Common Ancestor
- 03-28236. Lowest Common Ancestor of a Binary Tree 二叉树的最低公共祖先
- 03-28PAT A1143 Lowest Common Ancestor (30 分)
- 03-287-235. Lowest Common Ancestor of a Binary Search Tree
- 03-28letecode [235] - Lowest Common Ancestor of a Binary Search Tree
- 03-28Python3解leetcode Lowest Common Ancestor of a Binary Search TreeBinary Tree Paths
- 03-28PAT 1143 Lowest Common Ancestor (30 分)