235. Lowest Common Ancestor of a Binary Search Tree
2016-03-14 13:15
106 查看
/** * 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; if(root==p||root==q) return root; TreeNode* l=lowestCommonAncestor(root->left,p,q); TreeNode* r=lowestCommonAncestor(root->right,p,q); if(!l&&!r) return NULL; else if(!l&&r) return r; else if(l&&!r) return l; else return root; } };
相关文章推荐
- java实现smtp邮件功能
- .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序
- 写给想做主策的策划师们
- rem详解及使用方法
- 设计模式之装饰者模式
- 231. Power of Two
- 230. Kth Smallest Element in a BST(1)
- 226. Invert Binary Tree
- 217. Contains Duplicate
- Android Studio导入第三方jar包
- JSON.parse()和JSON.stringify() 的用法区别
- Android布局适配中一些显示问题
- uva 10891 Game of Sum
- 51Nod--1384全排列
- 206. Reverse Linked List
- 205. Isomorphic Strings
- 189. Rotate Array
- 172. Factorial Trailing Zeroes
- javascript处理日期
- 每个架构师都应该研究下康威定律