一天一道LeetCode(八). 235. 二叉搜索树的最近公共祖先 236. 二叉树的最近公共祖先
- 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]
示例 1:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
示例 2:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出: 5
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉树中。
思路:题解来自”极客时间“的《算法面试通关40讲》的”19 - 面试题:二叉树&二叉搜索树的最近公共祖先“,主要思路是递归思想
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if root == None: return root if root == p or root == q: return root leftnode = self.lowestCommonAncestor(root.left, p, q) rightnode = self.lowestCommonAncestor(root.right, p, q) if leftnode: if rightnode: return root else: return leftnode else: return rightnode
- 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。
题解:二叉搜索树的特点是左子树比父亲节点小,右子树比父亲节点大,所以需要遍历到root值在p,q之间即可
# 递归版本 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if root == None: return None if root.val > max(p.val, q.val): return self.lowestCommonAncestor(root.left, p, q) elif root.val < min(p.val, q.val): return self.lowestCommonAncestor(root.right, p, q) else: return root # 非递归版本 class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': while root: if root.val > max(p.val, q.val): root = root.left elif root.val < min(p.val, q.val): root = root.right else: return root return root
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
- Leetcode 235. 二叉搜索树的最近公共祖先
- LeetCode写题历程——235. 二叉搜索树的最近公共祖先
- leetcode 236. 二叉树的最近公共祖先
- Leetcode:236. 二叉树的最近公共祖先
- leetcode_235. Lowest Common Ancestor of a Binary Search Tree 查找二叉搜索树最近公共祖先结点
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树BST的最近公共祖先LCA + 深度优先遍历DFS
- Leetcode 236. 二叉树的最近公共祖先
- LeetCode 236. 二叉树的最近公共祖先
- 235. 二叉搜索树的最近公共祖先(二叉树)
- Leetcode 236.二叉树的最近公共祖先
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
- 二叉树中两个节点的最近公共祖先(leetcode)
- 找工作刷题记录_014二叉树&二叉搜索树最近的公共祖先
- 二叉树的最近公共祖先和二叉搜索树的最近公共祖先
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树 + 深度优先遍历DFS