[LeetCode][Python3] 236. Lowest Common Ancestor of a Binary Tree
                2019. 4. 1. 23:45 |
                
                    프로그래밍/LeetCode
                
            
            
            
        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)
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 148. Sort List (1) | 2019.04.04 | 
|---|---|
| [LeetCode][Python3] 139. Word Break (0) | 2019.04.03 | 
| [LeetCode][Python3] 56. Merge Intervals (0) | 2019.04.03 | 
| [LeetCode][Python3] 295. Find Median from Data Stream (0) | 2019.04.02 | 
| [LeetCode][Python3] 207. Course Schedule (0) | 2019.03.31 | 
| [LeetCode][Python3] 116. Populating Next Right Pointers in Each Node (0) | 2019.03.30 | 
| [LeetCode][Python3] 208. Implement Trie (Prefix Tree) (1) | 2019.03.30 | 
| [LeetCode][Python3] 239. Sliding Window Maximum (0) | 2019.03.29 | 
최근에 달린 댓글 최근에 달린 댓글