Skip to content

Commit 37ddf1d

Browse files
authored
Added tasks 199-224
1 parent 9ff0fa4 commit 37ddf1d

File tree

31 files changed

+857
-0
lines changed

31 files changed

+857
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# #Medium #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
2+
# #LeetCode_75_Binary_Tree/BFS #Data_Structure_II_Day_16_Tree #Level_2_Day_15_Tree
3+
# #Top_Interview_150_Binary_Tree_BFS #2025_09_14_Time_0_ms_(100.00%)_Space_17.76_MB_(60.72%)
4+
5+
from typing import List, Optional
6+
7+
class TreeNode:
8+
def __init__(self, val=0, left=None, right=None):
9+
self.val = val
10+
self.left = left
11+
self.right = right
12+
13+
14+
class Solution:
15+
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
16+
result = []
17+
self._recurse(root, 0, result)
18+
return result
19+
20+
def _recurse(self, node: Optional[TreeNode], level: int, result: List[int]):
21+
if node is not None:
22+
if len(result) < level + 1:
23+
result.append(node.val)
24+
self._recurse(node.right, level + 1, result)
25+
self._recurse(node.left, level + 1, result)
26+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from Solution0199 import Solution, TreeNode
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_rightSideView(self):
6+
left = TreeNode(2)
7+
left.right = TreeNode(5)
8+
right = TreeNode(3)
9+
right.right = TreeNode(4)
10+
root = TreeNode(1, left, right)
11+
self.assertEqual(Solution().rightSideView(root), [1, 3, 4])
12+
13+
def test_rightSideView2(self):
14+
root = TreeNode(1)
15+
root.right = TreeNode(3)
16+
self.assertEqual(Solution().rightSideView(root), [1, 3])
17+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
199\. Binary Tree Right Side View
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg)
10+
11+
**Input:** root = [1,2,3,null,5,null,4]
12+
13+
**Output:** [1,3,4]
14+
15+
**Example 2:**
16+
17+
**Input:** root = [1,null,3]
18+
19+
**Output:** [1,3]
20+
21+
**Example 3:**
22+
23+
**Input:** root = []
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range `[0, 100]`.
30+
* `-100 <= Node.val <= 100`
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
2+
# #Top_Interview_150_Bit_Manipulation #2025_09_14_Time_1_ms_(82.83%)_Space_18.01_MB_(19.55%)
3+
4+
class Solution:
5+
def __init__(self):
6+
# Precomputed masks for different bit positions
7+
self.masks = [
8+
0,
9+
0x80000000,
10+
0xC0000000,
11+
0xE0000000,
12+
0xF0000000,
13+
0xF8000000,
14+
0xFC000000,
15+
0xFE000000,
16+
0xFF000000,
17+
0xFF800000,
18+
0xFFC00000,
19+
0xFFE00000,
20+
0xFFF00000,
21+
0xFFF80000,
22+
0xFFFC0000,
23+
0xFFFE0000,
24+
0xFFFF0000,
25+
0xFFFF8000,
26+
0xFFFFC000,
27+
0xFFFFE000,
28+
0xFFFFF000,
29+
0xFFFFF800,
30+
0xFFFFFC00,
31+
0xFFFFFE00,
32+
0xFFFFFF00,
33+
0xFFFFFF80,
34+
0xFFFFFFC0,
35+
0xFFFFFFE0,
36+
0xFFFFFFF0,
37+
0xFFFFFFF8,
38+
0xFFFFFFFC,
39+
0xFFFFFFFE
40+
]
41+
42+
def rangeBitwiseAnd(self, left: int, right: int) -> int:
43+
if left == right:
44+
return left
45+
return right & self.masks[self._numberOfLeadingZeros(left ^ right)]
46+
47+
def _numberOfLeadingZeros(self, n: int) -> int:
48+
"""Count leading zeros in 32-bit integer"""
49+
if n == 0:
50+
return 32
51+
count = 0
52+
if n < 0:
53+
return 0
54+
while n > 0:
55+
n = n >> 1
56+
count += 1
57+
return 32 - count
58+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
from Solution0201 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_rangeBitwiseAnd(self):
6+
self.assertEqual(Solution().rangeBitwiseAnd(5, 7), 4)
7+
8+
def test_rangeBitwiseAnd2(self):
9+
self.assertEqual(Solution().rangeBitwiseAnd(0, 0), 0)
10+
11+
def test_rangeBitwiseAnd3(self):
12+
self.assertEqual(Solution().rangeBitwiseAnd(1, 2147483647), 0)
13+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
201\. Bitwise AND of Numbers Range
2+
3+
Medium
4+
5+
Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
6+
7+
**Example 1:**
8+
9+
**Input:** left = 5, right = 7
10+
11+
**Output:** 4
12+
13+
**Example 2:**
14+
15+
**Input:** left = 0, right = 0
16+
17+
**Output:** 0
18+
19+
**Example 3:**
20+
21+
**Input:** left = 1, right = 2147483647
22+
23+
**Output:** 0
24+
25+
**Constraints:**
26+
27+
* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# #Easy #Top_Interview_Questions #Hash_Table #Math #Two_Pointers #Algorithm_II_Day_21_Others
2+
# #Programming_Skills_I_Day_4_Loop #Level_2_Day_1_Implementation/Simulation
3+
# #Top_Interview_150_Hashmap #2025_09_15_Time_0_ms_(100.00%)_Space_17.57_MB_(97.85%)
4+
5+
class Solution:
6+
def isHappy(self, n: int) -> bool:
7+
a = n
8+
rem = 0
9+
sum_val = 0
10+
if a == 1 or a == 7:
11+
return True
12+
elif 1 < a < 10:
13+
return False
14+
else:
15+
while a != 0:
16+
rem = a % 10
17+
sum_val += rem * rem
18+
a //= 10
19+
if sum_val != 1:
20+
return self.isHappy(sum_val)
21+
else:
22+
return True
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
from Solution0202 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_isHappy(self):
6+
self.assertTrue(Solution().isHappy(19))
7+
8+
def test_isHappy2(self):
9+
self.assertFalse(Solution().isHappy(2))
10+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
202\. Happy Number
2+
3+
Easy
4+
5+
Write an algorithm to determine if a number `n` is happy.
6+
7+
A **happy number** is a number defined by the following process:
8+
9+
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
10+
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
11+
* Those numbers for which this process **ends in 1** are happy.
12+
13+
Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 19
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
1<sup>2</sup> + 9<sup>2</sup> = 82
24+
25+
8<sup>2</sup> + 2<sup>2</sup> = 68
26+
27+
6<sup>2</sup> + 8<sup>2</sup> = 100
28+
29+
1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1
30+
31+
**Example 2:**
32+
33+
**Input:** n = 2
34+
35+
**Output:** false
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>

0 commit comments

Comments
 (0)