235. Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:域名交易 知乎 编辑:程序博客网 时间:2024/05/16 19:51
两个数都小于根节点,都位于左子树,都大于都位于右子树。一大一小就是根节点。递归实现
public class Solution {
//2, 1
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(Math.max(p.val, q.val) < root.val) {
return lowestCommonAncestor(root.left, p, q);
} else if(Math.min(p.val, q.val) > root.val) {
return lowestCommonAncestor(root.right, p, q);
} else return root;
}
}
0 0
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235.Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235.Lowest Common Ancestor of a Binary Search Tree
- 235.Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree LeetCode
- 235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235. Lowest Common Ancestor of a Binary Search Tree
- 这只是第一篇日志,以后每天都会更新
- UVA 230
- 关于尺寸单位
- UVA 1596
- 169. Majority Element
- 235. Lowest Common Ancestor of a Binary Search Tree
- servlet API
- java基础函数
- 328. Odd Even Linked List
- hdoj5621KK's Point
- Spring10种常见异常解决方法
- Android音乐播放器高级开发
- apache commons io 可靠性报告
- #学习笔记#(39)JS随机颜色