LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:像牲口一样活下去 知乎 编辑:程序博客网 时间:2024/05/13 15:55
描述
BST中找最近公共父节点
解决
递归搜索
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root) return NULL; else if (root -> val > p -> val && root -> val > q -> val) return lowestCommonAncestor(root -> left, p, q); else if (root -> val < p -> val && root -> val < q -> 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
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree LeetCode
- 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
- [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
- 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.[Leetcode]Lowest Common Ancestor of a Binary Search Tree
- 【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
- SpringMVC框架整理(一)
- C++八大排序算法(一)
- R中设置图形参数--函数par()详解
- Django提交表单时遇到403错误:CSRF verification failed
- Cookie获取最后一次访问时间(浏览器第一次打开的时候是没有cookie的)
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- MySQL Delete、Truncate、updateDelete
- 小试2解析
- [BZOJ2541][Ctsc2000]冰原探险(bfs)
- 统计出现次数最多的数
- HTML
- c语言求最大公约数
- Linux学习笔记
- HTTP请求和响应过程