【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

解法一:递归

 1 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
 2 {
 3     if (root == NULL || p == NULL || q == NULL)
 4         return NULL;
 5
 6     if (root->val > p->val && root->val > q->val)
 7         return lowestCommonAncestor(root->left, p, q);
 8     else if (root->val < p->val && root->val < q->val)
 9         return lowestCommonAncestor(root->right, p, q);
10     return root;
11 }

解法二:迭代

 1 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
 2 {
 3     if (root == NULL || p == NULL || q == NULL)
 4         return NULL;
 5
 6     while (true) {
 7         if (root->val < p->val && root->val < q->val)
 8             root = root->right;
 9         else if (root->val > p->val && root->val > q->val)
10             root = root->left;
11         else
12             break;
13     }
14     return root;
15 }
时间: 2024-11-05 00:08:31

【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree的相关文章

LeetCode 235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree) 32

235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)." 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,nul

leetcode - Lowest Common Ancestor of a Binary Search Tree

leetcode -  Lowest Common Ancestor of a Binary Search Tree 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

[LeetCode][JavaScript]Lowest Common Ancestor of a Binary Search Tree

Lowest Common Ancestor of a Binary Search Tree 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

【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

Lowest Common Ancestor of a Binary Search Tree 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

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree 递归寻找p或q,如果找到,层层向上返回,知道 root 左边和右边都不为NULL:if (left!=NULL && right!=NULL) return root; 时间复杂度 O(n),空间复杂度 O(H) class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode*

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

Lowest Common Ancestor of a Binary Search Tree &amp; a Binary Tree

235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description 题目大意:给定一棵二叉查找树和两个节点p和q,要求返回这两个节点的第一个公共祖先. 思路:对于当前节点node,如果p和q的val都大于node的val,则说明p和q的第一个公共祖先在node的右子树,如

LeetCode_235. Lowest Common Ancestor of a Binary Search Tree

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 be

leetcode_235——Lowest Common Ancestor of a Binary Search Tree(二叉排序树)

Lowest Common Ancestor of a Binary Search Tree Total Accepted: 7402 Total Submissions: 19069My Submissions Question Solution Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the defin

235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

235. Lowest Common Ancestor of a Binary Search Tree 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