LeetCode(算法)- 235. 二叉搜索树的最近公共祖先
Posted 放羊的牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(算法)- 235. 二叉搜索树的最近公共祖先相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
- 亚马逊(Amazon)
- 谷歌(Google)
- 微软(Microsoft)
- 腾讯(Tenent)
- 快手
- 小米集团
- 苹果(Apple)
- 领英(LinkedIn)
AC 代码
- Java
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) val = x;
*
*/
// 解决方案(1)
class Solution
Stack<TreeNode> pStack = new Stack<>(), qStack = new Stack<>();
Map<TreeNode, Integer> pMap = new HashMap<>(), qMap = new HashMap<>();
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
dfs(root, p.val, pStack, pMap, 0);
dfs(root, q.val, qStack, qMap, 0);
TreeNode res = common();
return res;
TreeNode common()
TreeNode res;
while (!pStack.isEmpty() && !qStack.isEmpty())
TreeNode pe = pStack.peek();
TreeNode qe = qStack.peek();
if (pe.val == qe.val)
res = pStack.peek();
pStack.pop();
qStack.pop();
return res;
if (pMap.get(pe) > qMap.get(qe))
pStack.pop();
else if (pMap.get(pe) < qMap.get(qe))
qStack.pop();
else
pStack.pop();
qStack.pop();
return null;
void dfs(TreeNode node, int val, Stack<TreeNode> stack, Map<TreeNode, Integer> map, int l)
if (node == null)
return;
if (node.val == val)
stack.push(node);
map.put(node, l);
else if (node.val > val)
stack.push(node);
map.put(node, l);
dfs(node.left, val, stack, map, l + 1);
else
stack.push(node);
map.put(node, l);
dfs(node.right, val, stack, map, l + 1);
// 解决方案(2)
class Solution
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
if(p.val > q.val) // 保证 p.val < q.val
TreeNode tmp = p;
p = q;
q = tmp;
while(root != null)
if(root.val < p.val) // p,q 都在 root 的右子树中
root = root.right; // 遍历至右子节点
else if(root.val > q.val) // p,q 都在 root 的左子树中
root = root.left; // 遍历至左子节点
else break;
return root;
// 解决方案(3)
class Solution
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
if(root.val < p.val && root.val < q.val)
return lowestCommonAncestor(root.right, p, q);
if(root.val > p.val && root.val > q.val)
return lowestCommonAncestor(root.left, p, q);
return root;
- C++
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL)
* ;
*/
// 解决方案(1)
class Solution
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
if(p->val > q->val)
swap(p, q);
while(root != nullptr)
if(root->val < p->val) // p,q 都在 root 的右子树中
root = root->right; // 遍历至右子节点
else if(root->val > q->val) // p,q 都在 root 的左子树中
root = root->left; // 遍历至左子节点
else break;
return root;
;
// 解决方案(2)
class Solution
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
if(root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right, p, q);
if(root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left, p, q);
return root;
;
创作打卡挑战赛
赢取流量/现金/CSDN周边激励大奖
以上是关于LeetCode(算法)- 235. 二叉搜索树的最近公共祖先的主要内容,如果未能解决你的问题,请参考以下文章