Skip to content

Commit 37b9e58

Browse files
committed
feat: new color for sidebar
1 parent 4e1faa1 commit 37b9e58

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

craco.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module.exports = {
22
devServer: {
33
proxy: {
4-
'/api': {
4+
'/api/show-my-ip': {
55
target: 'https://api.ipify.org',
66
changeOrigin: true,
7-
pathRewrite: { '^/api': '' },
7+
pathRewrite: { '^/api/show-my-ip': '' },
88
},
99
},
1010
},

public/posts/algos/lowest-common-ancestor-of-a-binary-search-tree.txt

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This post will take you on a journey from the basics of trees to a specific type
77

88
## What is a Tree?
99

10-
In computer science, a **Tree** is a hierarchical data structure that consists of nodes connected by edges.
10+
In computer science, a **Tree** is a hierarchical data structure that consists of nodes connected by edges.
1111

1212
Unlike linear data structures like arrays or linked lists, trees are non-linear and are used to represent hierarchical relationships.
1313

@@ -27,7 +27,7 @@ Trees are used in various applications, such as file systems, organization chart
2727

2828
## Binary Search Trees (BSTs)
2929

30-
A **Binary Search Tree (BST)** is a special type of binary tree where the nodes are ordered in a specific way.
30+
A **Binary Search Tree (BST)** is a special type of binary tree where the nodes are ordered in a specific way.
3131

3232
This ordering makes operations like searching, insertion, and deletion very efficient.
3333

@@ -60,7 +60,7 @@ Let's explore how to implement some of these fundamental tree algorithms in Go.
6060

6161
### Finding the Height/Depth of a Binary Tree
6262

63-
The **height** of a binary tree is the number of edges on the longest path from the root node to a leaf node. A tree with only a root node has a height of 0.
63+
The **height** of a binary tree is the number of edges on the longest path from the root node to a leaf node. A tree with only a root node has a height of 0.
6464

6565
The concept is closely related to the **depth** of a node, which is its distance from the root. The height of the tree is, therefore, the maximum depth of any node in the tree.
6666

@@ -138,7 +138,7 @@ func postOrderTraversal(node *TreeNode) {
138138

139139
## LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
140140

141-
Now, let's apply our knowledge to a classic problem.
141+
Now, let's apply our knowledge to a classic problem.
142142

143143
The **Lowest Common Ancestor (LCA)** of two nodes, `p` and `q`, in a tree is the lowest (i.e., deepest) node that has both `p` and `q` as descendants.
144144

@@ -165,15 +165,15 @@ For example, consider the following BST:
165165

166166
### The Solution
167167

168-
The properties of a BST make finding the LCA particularly efficient.
168+
The properties of a BST make finding the LCA particularly efficient.
169169

170170
We can start at the root of the tree and use the values of `p` and `q` to decide where to go next.
171171

172172
Let's consider the current node we are at, let's call it `current`.
173173

174174
1. If both `p` and `q` are **greater** than `current.val`, it means that the LCA must be in the **right** subtree. So, we move to the right child.
175175
2. If both `p` and `q` are **less** than `current.val`, it means that the LCA must be in the **left** subtree. So, we move to the left child.
176-
3. If one of `p` or `q` is greater than `current.val` and the other is less than `current.val` (or if one of them is equal to `current.val`), then `current` is the LCA.
176+
3. If one of `p` or `q` is greater than `current.val` and the other is less than `current.val` (or if one of them is equal to `current.val`), then `current` is the LCA.
177177

178178
This is because `p` and `q` are on opposite sides of the current node, meaning it's the split point and thus the lowest common ancestor.
179179

@@ -233,12 +233,57 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
233233
}
234234
```
235235

236-
Both of these solutions are very efficient, with a time complexity of O(H), where H is the height of the tree. In a balanced BST, this is O(log N), where N is the number of nodes.
236+
Both of these solutions are very efficient, with a time complexity of O(H), where H is the height of the tree. In a balanced BST, this is O(log N), where N is the number of nodes.
237237

238238
The space complexity for the iterative solution is O(1), while the recursive solution has a space complexity of O(H) due to the call stack.
239239

240+
### Alternative: Stack-Based Solution
241+
242+
Another way to solve this problem is by finding the path from the root to each of the two nodes, `p` and `q`. Once we have both paths, we can compare them to find the last common node, which is the LCA.
243+
244+
This method is more generic and would also work for a regular binary tree, but it's less efficient for a BST than the previous solutions because it requires traversing parts of the tree multiple times and uses extra space to store the paths.
245+
246+
Here is the implementation in Go:
247+
248+
```go
249+
// Helper function to find the path from the root to a target node
250+
func getPath(root, target *TreeNode) []*TreeNode {
251+
path := []*TreeNode{}
252+
current := root
253+
for current != nil {
254+
path = append(path, current)
255+
if target.Val < current.Val {
256+
current = current.Left
257+
} else if target.Val > current.Val {
258+
current = current.Right
259+
} else {
260+
break // Found the target
261+
}
262+
}
263+
return path
264+
}
265+
266+
func lowestCommonAncestorWithStacks(root, p, q *TreeNode) *TreeNode {
267+
pathP := getPath(root, p)
268+
pathQ := getPath(root, q)
269+
270+
var lca *TreeNode
271+
272+
// Iterate through both paths until they diverge
273+
for i := 0; i < len(pathP) && i < len(pathQ); i++ {
274+
if pathP[i] == pathQ[i] {
275+
lca = pathP[i]
276+
} else {
277+
break
278+
}
279+
}
280+
281+
return lca
282+
}
283+
```
284+
240285
## Conclusion
241286

242-
Trees and Binary Search Trees are powerful data structures that are essential for any programmer's toolkit. By understanding their properties and the algorithms that operate on them, you can solve a wide range of problems efficiently.
287+
Trees and Binary Search Trees are powerful data structures that are essential for any programmer's toolkit. By understanding their properties and the algorithms that operate on them, you can solve a wide range of problems efficiently.
243288

244289
The Lowest Common Ancestor problem is a perfect example of how the structure of a BST can be leveraged to find an elegant and optimal solution.

public/rss.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@
99
<link>https://fezcode.com</link>
1010
</image>
1111
<generator>RSS for Node</generator>
12-
<lastBuildDate>Thu, 06 Nov 2025 22:19:40 GMT</lastBuildDate>
12+
<lastBuildDate>Thu, 06 Nov 2025 22:37:00 GMT</lastBuildDate>
1313
<atom:link href="https://fezcode.com/rss.xml" rel="self" type="application/rss+xml"/>
14-
<pubDate>Thu, 06 Nov 2025 22:19:40 GMT</pubDate>
14+
<pubDate>Thu, 06 Nov 2025 22:37:00 GMT</pubDate>
1515
<copyright><![CDATA[2025 Ahmed Samil Bulbul]]></copyright>
1616
<language><![CDATA[en]]></language>
1717
<managingEditor><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></managingEditor>
1818
<webMaster><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></webMaster>
1919
<ttl>60</ttl>
20+
<item>
21+
<title><![CDATA[Lowest Common Ancestor with Binary Search Tree]]></title>
22+
<description><![CDATA[[object Object]]]></description>
23+
<link>https://fezcode.com/#/blog/lca</link>
24+
<guid isPermaLink="false">https://fezcode.com/#/blog/lca</guid>
25+
<dc:creator><![CDATA[Ahmed Samil Bulbul]]></dc:creator>
26+
<pubDate>Fri, 07 Nov 2025 00:00:00 GMT</pubDate>
27+
<content:encoded><![CDATA[<h1>Understanding Trees, Binary Search Trees, and Finding the Lowest Common Ancestor</h1>
28+
<p><a href="https://fezcode.com/#/blog/lca">Read more...</a></p>]]></content:encoded>
29+
</item>
2030
<item>
2131
<title><![CDATA[Monotonic Stack with Daily Temperatures]]></title>
2232
<description><![CDATA[[object Object]]]></description>

src/components/Sidebar.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Sidebar = ({ isOpen, toggleSidebar, toggleModal }) => {
6565
const getLinkClass = ({ isActive }) =>
6666
`flex items-center space-x-3 px-3 py-1 rounded-md transition-colors ${
6767
isActive
68-
? 'text-primary-400 bg-gray-800'
68+
? 'text-article bg-article-alpha-10'
6969
: 'text-gray-100 hover:text-white hover:bg-gray-800'
7070
}`;
7171

@@ -120,10 +120,10 @@ const Sidebar = ({ isOpen, toggleSidebar, toggleModal }) => {
120120
<button
121121
onClick={() => setIsMainOpen(!isMainOpen)}
122122
className={`flex items-center justify-between w-full text-sm font-normal uppercase tracking-wider mb-4 focus:outline-none ${
123-
isMainActive ? 'text-red-400' : 'text-gray-100'
123+
isMainActive ? 'text-article' : 'text-gray-100'
124124
}`}
125125
>
126-
<span className={`flex items-center gap-2 font-sans ${isMainActive ? 'text-rose-400' : 'text-white'}`}>
126+
<span className={`flex items-center gap-2 font-sans ${isMainActive ? 'text-article' : 'text-white'}`}>
127127
<AsteriskSimpleIcon size={16} />
128128
<span>Main</span>
129129
</span>
@@ -155,10 +155,10 @@ const Sidebar = ({ isOpen, toggleSidebar, toggleModal }) => {
155155
<button
156156
onClick={() => setIsContentOpen(!isContentOpen)}
157157
className={`flex items-center justify-between w-full text-sm font-normal uppercase tracking-wider mb-4 focus:outline-none ${
158-
isContentActive ? 'text-red-400' : 'text-gray-100'
158+
isContentActive ? 'text-article' : 'text-gray-100'
159159
}`}
160160
>
161-
<span className={`flex items-center gap-2 font-sans ${isContentActive ? 'text-rose-400' : 'text-white'}`}>
161+
<span className={`flex items-center gap-2 font-sans ${isContentActive ? 'text-article' : 'text-white'}`}>
162162
<BooksIcon size={16} />
163163
<span>Content</span>
164164
</span>
@@ -196,10 +196,10 @@ const Sidebar = ({ isOpen, toggleSidebar, toggleModal }) => {
196196
<button
197197
onClick={() => setIsAppsOpen(!isAppsOpen)}
198198
className={`flex items-center justify-between w-full text-sm font-normal uppercase tracking-wider mb-4 focus:outline-none ${
199-
isAppsActive ? 'text-red-400' : 'text-gray-100'
199+
isAppsActive ? 'text-article' : 'text-gray-100'
200200
}`}
201201
>
202-
<span className={`flex items-center gap-2 font-sans ${isAppsActive ? 'text-rose-400' : 'text-white'}`}>
202+
<span className={`flex items-center gap-2 font-sans ${isAppsActive ? 'text-article' : 'text-white'}`}>
203203
<SquaresFourIcon size={16} />
204204
<span>Apps</span>
205205
</span>

src/pages/apps/IpPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function IpPage() {
1313
const { addToast } = useToast();
1414

1515
useEffect(() => {
16-
fetch('/api?format=json')
16+
fetch('/api/show-my-ip?format=json')
1717
.then(response => {
1818
if (!response.ok) {
1919
throw new Error('Network response was not ok');

0 commit comments

Comments
 (0)