Leetcode 235. Lowest Common Ancestor of a Binary Search Tree (Easy) (cpp)
来源:互联网 发布:mac直接进入win10 编辑:程序博客网 时间:2024/05/22 15:11
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree (Easy) (cpp)
Tag: Tree
Difficulty: Easy
/*235. Lowest Common Ancestor of a Binary Search Tree (Easy)Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.*//** * 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 (min(p -> val, q -> val) < root -> val && max(p -> val, q -> val) > root -> val) return root; else if (root -> val < min(p -> val, q -> val)) return lowestCommonAncestor(root -> right, p, q); else if (root -> val > max(p -> val, q -> val)) return lowestCommonAncestor(root -> left, p, q); else return root; } };
0 0
- Leetcode 235. Lowest Common Ancestor of a Binary Search Tree (Easy) (cpp)
- 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
- iOS中 Realm的学习与使用 韩俊强的博客
- 使用阿里云云服务器Ubuntu14.0.4系统安装mysql修改默认编码导致mysqld.sock消失的问题
- Python学习笔记19:Python图形用户界面编程
- Radar Installation(贪心)
- 第一章.对象导论之异常处理
- Leetcode 235. Lowest Common Ancestor of a Binary Search Tree (Easy) (cpp)
- ABAP cl_gui_splitter_container屏幕分割
- Ubuntu 16.04-codeblocks 汉化
- hdu4790Just Random
- UVa 12100 Printer Queue(习题5-7)
- [0.0] 什么是机器学习
- 线程 —— 通过 Callable 对象创建线程
- 两次BC反思
- pop(),push(),shift(),unshift()的作用和区别