프로그래밍/LeetCode
[LeetCode][Python3] 236. Lowest Common Ancestor of a Binary Tree
snoopybox
2019. 4. 1. 23:45
Problem :
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
My Solution :
# 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, p, q):
if root in (p, q, None):
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
return root if (left and right) else (left or right)