leetcode 235 二叉搜索树最近公共祖先

leetcode 235 二叉搜索树最近公共祖先

题目描述:

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。

解法一:自己的写法,贼傻

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        def findPath(root,num,tmp,path):
            if not root:
                return
            if root.val == num.val:
                path.append(tmp + [root])
                return
            findPath(root.left,num,tmp+[root],path)
            findPath(root.right,num,tmp+[root],path)
        minn = min(p.val, q.val)
        maxn = max(p.val, q.val)
        p_path = []
        q_path = []
        findPath(root,p,[],p_path)
        findPath(root,q,[],q_path)
        p_path = p_path[0]#[::-1]
        q_path = q_path[0]#[::-1]
        lenm = min(len(p_path),len(q_path))
        for i in range(0,lenm):
            if p_path[i].val == q_path[i].val:
                res = p_path[i]
        return res

网上的写法

# 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':
        min_ = min(p.val,q.val)
        max_ = max(p.val,q.val)
        if not root:
            return
        if min_ <= root.val <= max_:
            return root
        else:
            l = self.lowestCommonAncestor(root.left, p, q)
            r = self.lowestCommonAncestor(root.right, p, q)
            if l:
                return l
            if r:
                return r

原文地址:https://www.cnblogs.com/curtisxiao/p/11247858.html

时间: 2024-07-31 13:35:24

leetcode 235 二叉搜索树最近公共祖先的相关文章

Leetcode 235.二叉搜索树的公共祖先

二叉搜索树的公共祖先 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 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

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 235. 二叉搜索树的最近公共祖先(c++)

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 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 解释: 节点

LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意: root 是树结点对象 (TreeNode object),而不是数组. 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / 2 6 / \ 1 3 最小的差

LeetCode 把二叉搜索树转换为累加树(538)

第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 概念 二叉搜索树 二叉查找树(Binary Search Tree

Leetcode 验证二叉搜索树

给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. 示例 1: 输入: 2 / 1 3 输出: true 示例 2: 输入: 5 / 1 4 / 3 6 输出: false 解释: 输入为: [5,1,4,null,null,3,6].   根节点的值为 5 ,但是其右子节点值为 4 . 这道题我想的解答方案是错误的,后来参考了别人的 # De

235. 二叉搜索树的最近公共祖先

题目链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/submissions/ 解题思路: 如果p和q都小于root,去左边找就行. 如果p和q在两侧的,直接就是root,这个可以通过val来判断. 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * Tr

【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(ro

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

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