Skip to content

Commit 619d269

Browse files
authored
Added tasks 199-224
1 parent 234e667 commit 619d269

File tree

34 files changed

+1039
-0
lines changed

34 files changed

+1039
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Large diffs are not rendered by default.
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package s0199_binary_tree_right_side_view
2+
3+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #LeetCode_75_Binary_Tree/BFS #Data_Structure_II_Day_16_Tree #Level_2_Day_15_Tree
5+
// #Top_Interview_150_Binary_Tree_BFS #2025_05_22_Time_0_ms_(100.00%)_Space_4.29_MB_(85.87%)
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
func rightSideView(root *TreeNode) []int {
16+
var list []int
17+
recurse(root, 0, &list)
18+
return list
19+
}
20+
21+
func recurse(node *TreeNode, level int, list *[]int) {
22+
if node != nil {
23+
if len(*list) < level+1 {
24+
*list = append(*list, node.Val)
25+
}
26+
recurse(node.Right, level+1, list)
27+
recurse(node.Left, level+1, list)
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package s0199_binary_tree_right_side_view
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestRightSideView(t *testing.T) {
9+
left := &TreeNode{
10+
Val: 2,
11+
Right: &TreeNode{Val: 5},
12+
}
13+
right := &TreeNode{
14+
Val: 3,
15+
Right: &TreeNode{Val: 4},
16+
}
17+
root := &TreeNode{
18+
Val: 1,
19+
Left: left,
20+
Right: right,
21+
}
22+
assert.Equal(t, []int{1, 3, 4}, rightSideView(root))
23+
}
24+
25+
func TestRightSideView2(t *testing.T) {
26+
root := &TreeNode{
27+
Val: 1,
28+
Right: &TreeNode{Val: 3},
29+
}
30+
assert.Equal(t, []int{1, 3}, rightSideView(root))
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package s0199_binary_tree_right_side_view
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package s0201_bitwise_and_of_numbers_range
2+
3+
// #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
4+
// #Top_Interview_150_Bit_Manipulation #2025_05_22_Time_0_ms_(100.00%)_Space_6.79_MB_(39.31%)
5+
6+
import "math/bits"
7+
8+
var masks = []uint32{
9+
0,
10+
0x80000000,
11+
0xC0000000,
12+
0xE0000000,
13+
0xF0000000,
14+
0xF8000000,
15+
0xFC000000,
16+
0xFE000000,
17+
0xFF000000,
18+
0xFF800000,
19+
0xFFC00000,
20+
0xFFE00000,
21+
0xFFF00000,
22+
0xFFF80000,
23+
0xFFFC0000,
24+
0xFFFE0000,
25+
0xFFFF0000,
26+
0xFFFF8000,
27+
0xFFFFC000,
28+
0xFFFFE000,
29+
0xFFFFF000,
30+
0xFFFFF800,
31+
0xFFFFFC00,
32+
0xFFFFFE00,
33+
0xFFFFFF00,
34+
0xFFFFFF80,
35+
0xFFFFFFC0,
36+
0xFFFFFFE0,
37+
0xFFFFFFF0,
38+
0xFFFFFFF8,
39+
0xFFFFFFFC,
40+
0xFFFFFFFE,
41+
}
42+
43+
func rangeBitwiseAnd(left int, right int) int {
44+
if left == right {
45+
return left
46+
}
47+
return right & int(masks[bits.LeadingZeros32(uint32(left^right))])
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0201_bitwise_and_of_numbers_range
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestRangeBitwiseAnd(t *testing.T) {
9+
assert.Equal(t, 4, rangeBitwiseAnd(5, 7))
10+
}
11+
12+
func TestRangeBitwiseAnd2(t *testing.T) {
13+
assert.Equal(t, 0, rangeBitwiseAnd(0, 0))
14+
}
15+
16+
func TestRangeBitwiseAnd3(t *testing.T) {
17+
assert.Equal(t, 0, rangeBitwiseAnd(1, 2147483647))
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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:** 1<sup>2</sup> + 9<sup>2</sup> = 82 8<sup>2</sup> + 2<sup>2</sup> = 68 6<sup>2</sup> + 8<sup>2</sup> = 100 1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1
22+
23+
**Example 2:**
24+
25+
**Input:** n = 2
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0202_happy_number
2+
3+
// #Easy #Top_Interview_Questions #Hash_Table #Math #Two_Pointers #Algorithm_II_Day_21_Others
4+
// #Programming_Skills_I_Day_4_Loop #Level_2_Day_1_Implementation/Simulation
5+
// #Top_Interview_150_Hashmap #2025_05_22_Time_0_ms_(100.00%)_Space_3.94_MB_(86.55%)
6+
7+
func isHappy(n int) bool {
8+
if n == 1 || n == 7 {
9+
return true
10+
}
11+
if n > 1 && n < 10 {
12+
return false
13+
}
14+
sum := 0
15+
a := n
16+
for a != 0 {
17+
rem := a % 10
18+
sum += rem * rem
19+
a /= 10
20+
}
21+
if sum != 1 {
22+
return isHappy(sum)
23+
}
24+
return true
25+
}

0 commit comments

Comments
 (0)