Skip to content

Commit 0cf8e30

Browse files
Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
authored and
Zanger67/leetcode
committed
Updated markdown files
1 parent 2a30be8 commit 0cf8e30

11 files changed

+332
-130
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# **[LeetCode Records](https://leetcode.com/u/Zanger/)** (437 solved)
1+
# **[LeetCode Records](https://leetcode.com/u/Zanger/)** (439 solved)
22

33
> My LeetCode Profile: [Zanger](https://leetcode.com/u/Zanger/)
44
@@ -128,6 +128,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python,
128128
| 227 | [Basic Calculator II](<https://leetcode.com/problems/basic-calculator-ii>) | Medium | | [solution](<markdowns/_227. Basic Calculator II.md>) | py |
129129
| 230 | [Kth Smallest Element in a BST](<https://leetcode.com/problems/kth-smallest-element-in-a-bst>) | Medium | B75, N150 | [solution](<markdowns/_230. Kth Smallest Element in a BST.md>) | py |
130130
| 235 | [Lowest Common Ancestor of a Binary Search Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree>) | Medium | B75, N150 | [solution](<markdowns/_235. Lowest Common Ancestor of a Binary Search Tree.md>) | py |
131+
| 236 | [Lowest Common Ancestor of a Binary Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree>) | Medium | B75 | [solution](<markdowns/_236. Lowest Common Ancestor of a Binary Tree.md>) | py |
131132
| 238 | [Product of Array Except Self](<https://leetcode.com/problems/product-of-array-except-self>) | Medium | B75, N150 | [solution](<markdowns/_238. Product of Array Except Self.md>) | py |
132133
| 239 | [Sliding Window Maximum](<https://leetcode.com/problems/sliding-window-maximum>) | Hard | N150 | [solution](<markdowns/_239. Sliding Window Maximum.md>) | py |
133134
| 242 | [Valid Anagram](<https://leetcode.com/problems/valid-anagram>) | Easy | B75, N150 | [solution](<markdowns/_242. Valid Anagram.md>) | c, py |
@@ -346,6 +347,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python,
346347
| 1721 | [Swapping Nodes in a Linked List](<https://leetcode.com/problems/swapping-nodes-in-a-linked-list>) | Medium | | [solution](<markdowns/_1721. Swapping Nodes in a Linked List.md>) | c |
347348
| 1730 | [Shortest Path to Get Food](<https://leetcode.com/problems/shortest-path-to-get-food>) | Medium | | [solution](<markdowns/_1730. Shortest Path to Get Food.md>) | py |
348349
| 1736 | [Latest Time by Replacing Hidden Digits](<https://leetcode.com/problems/latest-time-by-replacing-hidden-digits>) | Easy | | [solution](<markdowns/_1736. Latest Time by Replacing Hidden Digits.md>) | c |
350+
| 1740 | [Find Distance in a Binary Tree](<https://leetcode.com/problems/find-distance-in-a-binary-tree>) | Medium | Weekly Premium | [solution](<markdowns/_1740. Find Distance in a Binary Tree.md>) | py |
349351
| 1743 | [Restore the Array From Adjacent Pairs](<https://leetcode.com/problems/restore-the-array-from-adjacent-pairs>) | Medium | | [solution](<markdowns/_1743. Restore the Array From Adjacent Pairs.md>) | py |
350352
| 1748 | [Sum of Unique Elements](<https://leetcode.com/problems/sum-of-unique-elements>) | Easy | | [solution](<markdowns/_1748. Sum of Unique Elements.md>) | c |
351353
| 1759 | [Count Number of Homogenous Substrings](<https://leetcode.com/problems/count-number-of-homogenous-substrings>) | Medium | | [solution](<markdowns/_1759. Count Number of Homogenous Substrings.md>) | c, java |

markdowns/Questions_By_Code_Length.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Calculations are based on the code files's byte sizes.
4949
| 84 | [Largest Rectangle in Histogram](<https://leetcode.com/problems/largest-rectangle-in-histogram>) | Hard | N150 | [solution](<_84. Largest Rectangle in Histogram.md>) | py | Jun 11, 2024 |
5050
| 3180 | Weekly Contest 401 - q3 - [Maximum Total Reward Using Operations I](<https://leetcode.com/problems/maximum-total-reward-using-operations-i>) | Medium | Contest | [solution](<_3180. Maximum Total Reward Using Operations I.md>) | c, py | Jul 07, 2024 |
5151
| 1660 | [Correct a Binary Tree](<https://leetcode.com/problems/correct-a-binary-tree>) | Medium | | [solution](<_1660. Correct a Binary Tree.md>) | py | Jun 27, 2024 |
52+
| 1740 | [Find Distance in a Binary Tree](<https://leetcode.com/problems/find-distance-in-a-binary-tree>) | Medium | Weekly Premium | [solution](<_1740. Find Distance in a Binary Tree.md>) | py | Jul 15, 2024 |
5253
| 273 | [Integer to English Words](<https://leetcode.com/problems/integer-to-english-words>) | Hard | | [solution](<_273. Integer to English Words.md>) | py | Jun 11, 2024 |
5354
| 2206 | [Divide Array Into Equal Pairs](<https://leetcode.com/problems/divide-array-into-equal-pairs>) | Easy | | [solution](<_2206. Divide Array Into Equal Pairs.md>) | c, py | Jun 08, 2024 |
5455
| 1087 | [Brace Expansion](<https://leetcode.com/problems/brace-expansion>) | Medium | | [solution](<_1087. Brace Expansion.md>) | py | Jun 15, 2024 |
@@ -151,6 +152,7 @@ Calculations are based on the code files's byte sizes.
151152
| 79 | [Word Search](<https://leetcode.com/problems/word-search>) | Medium | B75, N150 | [solution](<_79. Word Search.md>) | py | Jun 27, 2024 |
152153
| 1265 | [Print Immutable Linked List in Reverse](<https://leetcode.com/problems/print-immutable-linked-list-in-reverse>) | Medium | | [solution](<_1265. Print Immutable Linked List in Reverse.md>) | c, py | Jun 06, 2024 |
153154
| 251 | [Flatten 2D Vector](<https://leetcode.com/problems/flatten-2d-vector>) | Medium | | [solution](<_251. Flatten 2D Vector.md>) | py | May 22, 2024 |
155+
| 236 | [Lowest Common Ancestor of a Binary Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree>) | Medium | B75 | [solution](<_236. Lowest Common Ancestor of a Binary Tree.md>) | py | Jul 15, 2024 |
154156
| 409 | [Longest Palindrome](<https://leetcode.com/problems/longest-palindrome>) | Easy | Daily | [solution](<_409. Longest Palindrome.md>) | c, py | Jun 04, 2024 |
155157
| 48 | [Rotate Image](<https://leetcode.com/problems/rotate-image>) | Medium | B75, N150 | [solution](<_48. Rotate Image.md>) | c, py | Jun 15, 2024 |
156158
| 2582 | [Pass the Pillow](<https://leetcode.com/problems/pass-the-pillow>) | Easy | Daily | [solution](<_2582. Pass the Pillow.md>) | c, cpp, cs, java, js, py | Jul 06, 2024 |

markdowns/Questions_By_Recent.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Calculations are based on the date of the first solve.
66

77
| # | Title | Level | Cats | Solution | Languages | Date Complete |
88
|-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:---------------------------------------------------------------------------------------------------|:---------------------------------|:----------------|
9+
| 236 | [Lowest Common Ancestor of a Binary Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree>) | Medium | B75 | [solution](<_236. Lowest Common Ancestor of a Binary Tree.md>) | py | Jul 15, 2024 |
10+
| 1740 | [Find Distance in a Binary Tree](<https://leetcode.com/problems/find-distance-in-a-binary-tree>) | Medium | Weekly Premium | [solution](<_1740. Find Distance in a Binary Tree.md>) | py | Jul 15, 2024 |
911
| 2196 | [Create Binary Tree From Descriptions](<https://leetcode.com/problems/create-binary-tree-from-descriptions>) | Medium | Daily | [solution](<_2196. Create Binary Tree From Descriptions.md>) | py | Jul 15, 2024 |
1012
| 735 | [Asteroid Collision](<https://leetcode.com/problems/asteroid-collision>) | Medium | | [solution](<_735. Asteroid Collision.md>) | py | Jul 14, 2024 |
1113
| 2211 | [Count Collisions on a Road](<https://leetcode.com/problems/count-collisions-on-a-road>) | Medium | | [solution](<_2211. Count Collisions on a Road.md>) | py | Jul 14, 2024 |

markdowns/Topics.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
------
66

77
- [Array](<by_topic/Array.md>) (197 completed)
8-
- [Hash Table](<by_topic/Hash Table.md>) (98 completed)
8+
- [Hash Table](<by_topic/Hash Table.md>) (99 completed)
99
- [String](<by_topic/String.md>) (96 completed)
10-
- [Depth-First Search](<by_topic/Depth-First Search.md>) (68 completed)
11-
- [Tree](<by_topic/Tree.md>) (67 completed)
12-
- [Binary Tree](<by_topic/Binary Tree.md>) (62 completed)
10+
- [Depth-First Search](<by_topic/Depth-First Search.md>) (70 completed)
11+
- [Tree](<by_topic/Tree.md>) (69 completed)
12+
- [Binary Tree](<by_topic/Binary Tree.md>) (64 completed)
1313
- [Sorting](<by_topic/Sorting.md>) (60 completed)
1414
- [Math](<by_topic/Math.md>) (54 completed)
15-
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (47 completed)
15+
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (48 completed)
1616
- [Two Pointers](<by_topic/Two Pointers.md>) (46 completed)
1717
- [Stack](<by_topic/Stack.md>) (45 completed)
1818
- [Linked List](<by_topic/Linked List.md>) (40 completed)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 1740. [Find Distance in a Binary Tree](<https://leetcode.com/problems/find-distance-in-a-binary-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 15, 2024*
10+
>
11+
> *Last updated : July 15, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Hash Table](<by_topic/Hash Table.md>), [Tree](<by_topic/Tree.md>), [Depth-First Search](<by_topic/Depth-First Search.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **71.487 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [m1740 v1 Weekly Premium.py](<../my-submissions/m1740 v1 Weekly Premium.py>)
24+
- [m1740 v2 Using Set Path.py](<../my-submissions/m1740 v2 Using Set Path.py>)
25+
### Python
26+
#### [m1740 v1 Weekly Premium.py](<../my-submissions/m1740 v1 Weekly Premium.py>)
27+
```Python
28+
# Definition for a binary tree node.
29+
# class TreeNode:
30+
# def __init__(self, val=0, left=None, right=None):
31+
# self.val = val
32+
# self.left = left
33+
# self.right = right
34+
class Solution:
35+
def findDistance(self, root: Optional[TreeNode], p: int, q: int) -> int:
36+
path = [root]
37+
pPath = []
38+
qPath = []
39+
40+
def dfs(path: List[Optional[TreeNode]],
41+
pPath: List[Optional[TreeNode]],
42+
qPath: List[Optional[TreeNode]]) -> None :
43+
if pPath and qPath :
44+
return
45+
46+
if path[-1].val == p :
47+
pPath.extend(path)
48+
if path[-1].val == q :
49+
qPath.extend(path)
50+
51+
if path[-1].left :
52+
path.append(path[-1].left)
53+
dfs(path, pPath, qPath)
54+
path.pop()
55+
if path[-1].right :
56+
path.append(path[-1].right)
57+
dfs(path, pPath, qPath)
58+
path.pop()
59+
60+
dfs(path, pPath, qPath)
61+
cnter = 0
62+
63+
while len(pPath) < len(qPath) :
64+
cnter += 1
65+
qPath.pop()
66+
while len(pPath) > len(qPath) :
67+
cnter += 1
68+
pPath.pop()
69+
70+
while pPath[-1] != qPath[-1] :
71+
cnter += 2
72+
pPath.pop()
73+
qPath.pop()
74+
75+
return cnter
76+
```
77+
78+
#### [m1740 v2 Using Set Path.py](<../my-submissions/m1740 v2 Using Set Path.py>)
79+
```Python
80+
# Definition for a binary tree node.
81+
# class TreeNode:
82+
# def __init__(self, val=0, left=None, right=None):
83+
# self.val = val
84+
# self.left = left
85+
# self.right = right
86+
class Solution:
87+
def findDistance(self, root: Optional[TreeNode], p: int, q: int) -> int:
88+
path = [root]
89+
pPath = set()
90+
qPath = set()
91+
92+
def dfs(path: List[Optional[TreeNode]],
93+
pPath: Set[Optional[TreeNode]],
94+
qPath: Set[Optional[TreeNode]]) -> None :
95+
if pPath and qPath :
96+
return
97+
98+
if path[-1].val == p :
99+
pPath.update(path)
100+
if path[-1].val == q :
101+
qPath.update(path)
102+
103+
if path[-1].left :
104+
path.append(path[-1].left)
105+
dfs(path, pPath, qPath)
106+
path.pop()
107+
if path[-1].right :
108+
path.append(path[-1].right)
109+
dfs(path, pPath, qPath)
110+
path.pop()
111+
112+
dfs(path, pPath, qPath)
113+
114+
return len(pPath) + len(qPath) - len(pPath & qPath) * 2
115+
```
116+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 236. [Lowest Common Ancestor of a Binary Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 15, 2024*
10+
>
11+
> *Last updated : July 15, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Depth-First Search](<by_topic/Depth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **62.906 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [m236.py](<../my-submissions/m236.py>)
24+
### Python
25+
#### [m236.py](<../my-submissions/m236.py>)
26+
```Python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, x):
30+
# self.val = x
31+
# self.left = None
32+
# self.right = None
33+
34+
class Solution:
35+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
36+
path = [root]
37+
pPath = []
38+
qPath = []
39+
40+
def dfs(path: List['TreeNode'], qPath: List['TreeNode'], pPath: List['TreeNode']) -> None :
41+
if qPath and pPath :
42+
return
43+
44+
if path[-1] == p :
45+
pPath.extend(path)
46+
if path[-1] == q :
47+
qPath.extend(path)
48+
49+
if path[-1].left :
50+
path.append(path[-1].left)
51+
dfs(path, qPath, pPath)
52+
path.pop()
53+
if path[-1].right :
54+
path.append(path[-1].right)
55+
dfs(path, qPath, pPath)
56+
path.pop()
57+
58+
dfs(path, qPath, pPath)
59+
60+
while len(qPath) < len(pPath) :
61+
pPath.pop()
62+
63+
while len(qPath) > len(pPath) :
64+
qPath.pop()
65+
66+
while qPath[-1] != pPath[-1] :
67+
qPath.pop()
68+
pPath.pop()
69+
70+
return pPath[-1]
71+
```
72+

0 commit comments

Comments
 (0)