leetcode:235. Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:ip更换软件win10 编辑:程序博客网 时间:2024/05/16 18:10
个人学习笔记:在二叉搜索树中寻找最低共同父节点
很简单的题目,直接贴代码
/** * 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 == NULL){ return root; } int min = p->val > q->val ? q->val : p->val; int max = p->val > q->val ? p->val : q->val; if (root->val >= min && root->val <= max) { return root; } if (root->val < min) { return lowestCommonAncestor(root->right, p, q); } if (root->val > max) { return lowestCommonAncestor(root->left, p, q); } }};
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
- Oracle 分区表
- 基于Ecshop 的移动端商品分类页面总结
- Web报表工具FineReport的JS API开发(一)
- 深入分析Linux kernel exception框架---案例分析
- shiro session绑定
- leetcode:235. Lowest Common Ancestor of a Binary Search Tree
- 关于asp输出json对象的方法及实例
- codeforces_622A. Infinite Sequence
- 【leetcode】【228】Summary Ranges
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
- day06 页面跳转和数据传递笔记
- 免费的论文查重网站
- 常用加密算法的Java实现
- js调用屏幕宽度