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


#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        stack = [root]
        if p.val>q.val:
            p,q=q,p

        while stack:
            node = stack.pop()
            if not node:
                continue
            if p.val<=node.val<=q.val:
                return node
            else:
                stack.append(node.left)
                stack.append(node.right)

Author: Lic
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Lic !
  TOC