235. Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:淘宝生死狙击卖号平台 编辑:程序博客网 时间:2024/05/20 05:08
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 5
For 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.
s思路:
1. 和236不同的地方是,这棵树是bst, 那么搜索的复杂度就应该是o(height).
2. 先看recursive。从根节点出发,分别去搜索两个节点,但这里搜索不用瞎搜索,需要利用BST的性质,比较p->val,q->val和root->val的关系,决定往左边搜还是右边搜。如果root->val>p->val同时root->val>q->val,说明p,q都在root的左侧,因此把root=root->left;如果root->val< p->val同时root->val < q->val,说明p,q都在root的右侧,因此把root=root->right;如果root->val>p->val同时root->val< q->val,或root->val< p->val同时root->val> q->val说明p,q在root的两侧,说明root就是LCA.
3. 再看iterative. 仿佛就是从root一路比较和p,q的值的关系,决定往左还是往右走,而且每次比较都是逐渐靠近答案,不用走回头路,因此没必要用stack吗?写完代码,确实就是不需要stack.
4. 通过这道题和236题,可以打破思维的这个定式:认为iterative必须要用一个stack,不然都不意思和人打交道,用queue都不行,这个确实是自己给自己不小心就设置了这么一个限制,所以思维应该打破这层束缚,而认识到iterative不但可以用stack,用queue也没错,不用任何数据结构更可以的!
5. iterative用stack是为了做dfs,比如:in-order,pre-order;用queue则是做bfs,保存一层的节点;不用的话,就是这道题,一路往下坚定的走向正确答案,每一层只需尝试一个节点,不走回头路!
6. 思维里的限制越少,想问题就越自由!打破思维的墙,任重道远!
//方法1:recursive。利用bst性质,选branch去尝试,复杂度o(height)class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { // if(!root||root==p||root==q) return root; 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); return root; }};//方法2:iterative。一路往下,逐渐离正确答案靠近,每次尝试都是正确的,不会走回头路//也就是不需要利用stack来穿越不同的层级,也不需要queue,因为每一层只需要遍历一个node,因此没必要用queue来保存同一层的节点!class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { // while(root){ if(root->val>p->val&&root->val>q->val) root=root->left; else if(root->val<p->val&&root->val<q->val) root=root->right; else return root; } return root; }};
- 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
- 【数据结构与算法】二叉搜索树 及 常用操作
- CSDN如何转载别人的文章
- making a list of numbers vim
- vim 行首行尾加字符
- Linux命令——1、基本的bash shell
- 235. Lowest Common Ancestor of a Binary Search Tree
- Redis 3.0集群搭建测试(一)
- Redis 3.0集群搭建测试(二)
- 237. Delete Node in a Linked List
- Redis 订阅与发布
- 如果交叉编译工具出现 没有那个文件或目录
- Redis Java客户端Jredis
- LeetCode - Two Sum
- 文档格式之间的转换使用