From 99c35b25c446694feee71644d0c381cda512d0ef Mon Sep 17 00:00:00 2001 From: YOUNGJIN NA <120540450+ppxyn1@users.noreply.github.com> Date: Sat, 31 Jan 2026 17:18:37 +0900 Subject: [PATCH 1/3] [:solved] #249 --- .../ppxyn1.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/ppxyn1.py diff --git a/lowest-common-ancestor-of-a-binary-search-tree/ppxyn1.py b/lowest-common-ancestor-of-a-binary-search-tree/ppxyn1.py new file mode 100644 index 0000000000..7357bdeab4 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/ppxyn1.py @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +# idea: BST property +# Time Complexity : O(long n) +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + cur = root + while cur: + if p.val > cur.val and q.val > cur.val: + cur = cur.right + elif p.val < cur.val and q.val < cur.val: + cur = cur.left + else: + return cur + + + + From 570cca07cdf5e6ede8c06fdefe434167f0fdcfc9 Mon Sep 17 00:00:00 2001 From: YOUNGJIN NA <120540450+ppxyn1@users.noreply.github.com> Date: Sun, 1 Feb 2026 15:50:10 +0900 Subject: [PATCH 2/3] [:solved] #252 --- kth-smallest-element-in-a-bst/ppxyn1.py | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/ppxyn1.py diff --git a/kth-smallest-element-in-a-bst/ppxyn1.py b/kth-smallest-element-in-a-bst/ppxyn1.py new file mode 100644 index 0000000000..9991e54e80 --- /dev/null +++ b/kth-smallest-element-in-a-bst/ppxyn1.py @@ -0,0 +1,31 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + +#idea : DFS (inorder) +#Time Complexity: O(n) + +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + stack = [] + cnt = 0 + curr = root + if not curr: + return + + while curr or stack: + stack.append(curr) + curr = curr.left + curr = stack.pop() + cnt += 1 + + if cnt == k: + return curr.val + + curr = curr.right + + + From 8ad7c1788c42d4c783f3353fb594a8ed980ce57f Mon Sep 17 00:00:00 2001 From: YOUNGJIN NA <120540450+ppxyn1@users.noreply.github.com> Date: Sun, 1 Feb 2026 15:50:51 +0900 Subject: [PATCH 3/3] [:fixed] --- kth-smallest-element-in-a-bst/ppxyn1.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kth-smallest-element-in-a-bst/ppxyn1.py b/kth-smallest-element-in-a-bst/ppxyn1.py index 9991e54e80..0baf399124 100644 --- a/kth-smallest-element-in-a-bst/ppxyn1.py +++ b/kth-smallest-element-in-a-bst/ppxyn1.py @@ -17,6 +17,7 @@ def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: return while curr or stack: + while curr: stack.append(curr) curr = curr.left curr = stack.pop() @@ -27,5 +28,3 @@ def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: curr = curr.right - -