Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0c7fa6

Browse files
authored
Added tasks 101-136
1 parent59c94ba commitf0c7fa6

File tree

33 files changed

+865
-1
lines changed

33 files changed

+865
-1
lines changed

‎README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
356356

357357
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
358358
|-|-|-|-|-|-
359+
| 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21
359360

360361
####Day 8
361362

@@ -484,6 +485,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
484485

485486
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
486487
|-|-|-|-|-|-
488+
| 0104 |[Maximum Depth of Binary Tree](src/main/ruby/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 55 | 96.72
487489

488490
####Day 11 Containers and Libraries
489491

@@ -751,11 +753,13 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
751753

752754
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
753755
|-|-|-|-|-|-
756+
| 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21
754757

755758
####Day 6 Tree
756759

757760
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
758761
|-|-|-|-|-|-
762+
| 0102 |[Binary Tree Level Order Traversal](src/main/ruby/g0101_0200/s0102_binary_tree_level_order_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 66 | 76.43
759763

760764
####Day 7 Binary Search
761765

@@ -887,6 +891,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
887891

888892
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
889893
|-|-|-|-|-|-
894+
| 0101 |[Symmetric Tree](src/main/ruby/g0101_0200/s0101_symmetric_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 58 | 91.34
890895

891896
####Day 16 Design
892897

@@ -922,6 +927,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
922927

923928
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
924929
|-|-|-|-|-|-
930+
| 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44
925931
| 0007 |[Reverse Integer](src/main/ruby/g0001_0100/s0007_reverse_integer/solution.rb)| Medium | Top_Interview_Questions, Math | 58 | 75.27
926932
| 0009 |[Palindrome Number](src/main/ruby/g0001_0100/s0009_palindrome_number/solution.rb)| Easy | Math | 83 | 74.19
927933

@@ -944,6 +950,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
944950

945951
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
946952
|-|-|-|-|-|-
953+
| 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21
947954
| 0001 |[Two Sum](src/main/ruby/g0001_0100/s0001_two_sum/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 57 | 89.38
948955
| 0055 |[Jump Game](src/main/ruby/g0001_0100/s0055_jump_game/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 88 | 95.88
949956
| 0075 |[Sort Colors](src/main/ruby/g0001_0100/s0075_sort_colors/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 54 | 88.46
@@ -980,6 +987,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
980987

981988
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
982989
|-|-|-|-|-|-
990+
| 0114 |[Flatten Binary Tree to Linked List](src/main/ruby/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.rb)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 47 | 100.00
983991
| 0024 |[Swap Nodes in Pairs](src/main/ruby/g0001_0100/s0024_swap_nodes_in_pairs/solution.rb)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 59 | 76.00
984992
| 0021 |[Merge Two Sorted Lists](src/main/ruby/g0001_0100/s0021_merge_two_sorted_lists/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 66 | 59.01
985993
| 0025 |[Reverse Nodes in k-Group](src/main/ruby/g0001_0100/s0025_reverse_nodes_in_k_group/solution.rb)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 62 | 91.67
@@ -989,6 +997,9 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
989997
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
990998
|-|-|-|-|-|-
991999
| 0094 |[Binary Tree Inorder Traversal](src/main/ruby/g0001_0100/s0094_binary_tree_inorder_traversal/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 56 | 84.00
1000+
| 0102 |[Binary Tree Level Order Traversal](src/main/ruby/g0101_0200/s0102_binary_tree_level_order_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 66 | 76.43
1001+
| 0104 |[Maximum Depth of Binary Tree](src/main/ruby/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 55 | 96.72
1002+
| 0124 |[Binary Tree Maximum Path Sum](src/main/ruby/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.rb)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 71 | 98.00
9921003
| 0098 |[Validate Binary Search Tree](src/main/ruby/g0001_0100/s0098_validate_binary_search_tree/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 68 | 70.23
9931004

9941005
####Udemy Trie and Heap
@@ -1048,6 +1059,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
10481059

10491060
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
10501061
|-|-|-|-|-|-
1062+
| 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21
10511063

10521064
####Day 4 Array
10531065

@@ -1092,6 +1104,9 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
10921104

10931105
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
10941106
|-|-|-|-|-|-
1107+
| 0102 |[Binary Tree Level Order Traversal](src/main/ruby/g0101_0200/s0102_binary_tree_level_order_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 66 | 76.43
1108+
| 0104 |[Maximum Depth of Binary Tree](src/main/ruby/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 55 | 96.72
1109+
| 0101 |[Symmetric Tree](src/main/ruby/g0101_0200/s0101_symmetric_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 58 | 91.34
10951110

10961111
####Day 12 Tree
10971112

@@ -1115,6 +1130,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
11151130

11161131
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
11171132
|-|-|-|-|-|-
1133+
| 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44
11181134
| 0015 |[3Sum](src/main/ruby/g0001_0100/s0015_3sum/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 659 | 83.74
11191135

11201136
####Day 2 Array
@@ -1194,6 +1210,7 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
11941210

11951211
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
11961212
|-|-|-|-|-|-
1213+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ruby/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 71 | 91.78
11971214

11981215
####Day 16 Tree
11991216

@@ -1302,11 +1319,22 @@ Ruby-based LeetCode algorithm problem solutions, regularly updated.
13021319

13031320
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
13041321
|-|-|-|-|-|-
1322+
| 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44
13051323

13061324
##Algorithms
13071325

13081326
| # | Title | Difficulty | Tag | Time, ms | Time, %
13091327
|------|----------------|-------------|-------------|----------|---------
1328+
| 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44
1329+
| 0131 |[Palindrome Partitioning](src/main/ruby/g0101_0200/s0131_palindrome_partitioning/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 1192 | 63.64
1330+
| 0128 |[Longest Consecutive Sequence](src/main/ruby/g0101_0200/s0128_longest_consecutive_sequence/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 135 | 91.36
1331+
| 0124 |[Binary Tree Maximum Path Sum](src/main/ruby/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.rb)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 71 | 98.00
1332+
| 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21
1333+
| 0114 |[Flatten Binary Tree to Linked List](src/main/ruby/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.rb)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 47 | 100.00
1334+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ruby/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 71 | 91.78
1335+
| 0104 |[Maximum Depth of Binary Tree](src/main/ruby/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 55 | 96.72
1336+
| 0102 |[Binary Tree Level Order Traversal](src/main/ruby/g0101_0200/s0102_binary_tree_level_order_traversal/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 66 | 76.43
1337+
| 0101 |[Symmetric Tree](src/main/ruby/g0101_0200/s0101_symmetric_tree/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 58 | 91.34
13101338
| 0098 |[Validate Binary Search Tree](src/main/ruby/g0001_0100/s0098_validate_binary_search_tree/solution.rb)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(log(N)) | 68 | 70.23
13111339
| 0096 |[Unique Binary Search Trees](src/main/ruby/g0001_0100/s0096_unique_binary_search_trees/solution.rb)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 57 | 61.54
13121340
| 0094 |[Binary Tree Inorder Traversal](src/main/ruby/g0001_0100/s0094_binary_tree_inorder_traversal/solution.rb)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 56 | 84.00

‎src/main/ruby/com_github_leetcode/tree_node.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,50 @@
22
classTreeNode
33
attr_accessor:val,:left,:right
44

5-
definitialize(val=0,left=nil,right=nil)
5+
definitialize(val,left=nil,right=nil)
66
@val=val
77
@left=left
88
@right=right
99
end
10+
11+
defself.create(tree_values)
12+
returnniliftree_values.empty?
13+
14+
root=TreeNode.new(tree_values[0])
15+
queue=[root]
16+
i=1
17+
18+
whilei <tree_values.length
19+
curr=queue.shift
20+
21+
iftree_values[i]
22+
curr.left=TreeNode.new(tree_values[i])
23+
queue.push(curr.left)
24+
end
25+
26+
i +=1
27+
28+
ifi <tree_values.length &&tree_values[i]
29+
curr.right=TreeNode.new(tree_values[i])
30+
queue.push(curr.right)
31+
end
32+
33+
i +=1
34+
end
35+
36+
root
37+
end
38+
39+
defto_s
40+
returnval.to_sifleft.nil? &&right.nil?
41+
42+
root=val.to_s
43+
left_value='null'
44+
right_value='null'
45+
46+
left_value=left.to_sifleft
47+
right_value=right.to_sifright
48+
49+
"#{root},#{left_value},#{right_value}"
50+
end
1051
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
101\. Symmetric Tree
2+
3+
Easy
4+
5+
Given the`root` of a binary tree,_check whether it is a mirror of itself_ (i.e., symmetric around its center).
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
10+
11+
**Input:** root =[1,2,2,3,4,4,3]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg)
18+
19+
**Input:** root =[1,2,2,null,3,null,3]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range`[1, 1000]`.
26+
*`-100 <= Node.val <= 100`
27+
28+
**Follow up:** Could you solve it both recursively and iteratively?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
2+
# #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_2_Day_15_Tree
3+
# #Big_O_Time_O(N)_Space_O(log(N)) #2023_11_23_Time_58_ms_(91.34%)_Space_211.1_MB_(48.82%)
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode
7+
# attr_accessor :val, :left, :right
8+
# def initialize(val = 0, left = nil, right = nil)
9+
# @val = val
10+
# @left = left
11+
# @right = right
12+
# end
13+
# end
14+
# @param {TreeNode} root
15+
# @return {Boolean}
16+
defis_symmetric(root)
17+
returntrueifroot.nil?
18+
19+
helper_symmetric(root.left,root.right)
20+
end
21+
22+
private
23+
24+
defhelper_symmetric(left_node,right_node)
25+
returnleft_node.nil? &&right_node.nil?ifleft_node.nil? ||right_node.nil?
26+
27+
returnfalseifleft_node.val !=right_node.val
28+
29+
helper_symmetric(left_node.left,right_node.right) &&helper_symmetric(left_node.right,right_node.left)
30+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
102\. Binary Tree Level Order Traversal
2+
3+
Medium
4+
5+
Given the`root` of a binary tree, return_the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
10+
11+
**Input:** root =[3,9,20,null,null,15,7]
12+
13+
**Output:**[[3],[9,20],[15,7]]
14+
15+
**Example 2:**
16+
17+
**Input:** root =[1]
18+
19+
**Output:**[[1]]
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, 2000]`.
30+
*`-1000 <= Node.val <= 1000`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Breadth_First_Search #Tree
2+
# #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_1_Day_6_Tree #Udemy_Tree_Stack_Queue
3+
# #Big_O_Time_O(N)_Space_O(N) #2023_11_23_Time_66_ms_(76.43%)_Space_211.3_MB_(57.86%)
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode
7+
# attr_accessor :val, :left, :right
8+
# def initialize(val = 0, left = nil, right = nil)
9+
# @val = val
10+
# @left = left
11+
# @right = right
12+
# end
13+
# end
14+
# @param {TreeNode} root
15+
# @return {Integer[][]}
16+
deflevel_order(root)
17+
result=[]
18+
returnresultifroot.nil?
19+
20+
queue=[root,nil]
21+
level=[]
22+
23+
untilqueue.empty?
24+
current=queue.shift
25+
26+
ifcurrent.nil?
27+
result <<level.dup
28+
level.clear
29+
queue.push(nil)unlessqueue.empty?
30+
else
31+
level <<current.val
32+
33+
queue.push(current.left)ifcurrent.left
34+
queue.push(current.right)ifcurrent.right
35+
end
36+
end
37+
38+
result
39+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
104\. Maximum Depth of Binary Tree
2+
3+
Easy
4+
5+
Given the`root` of a binary tree, return_its maximum depth_.
6+
7+
A binary tree's**maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
12+
13+
**Input:** root =[3,9,20,null,null,15,7]
14+
15+
**Output:** 3
16+
17+
**Example 2:**
18+
19+
**Input:** root =[1,null,2]
20+
21+
**Output:** 2
22+
23+
**Example 3:**
24+
25+
**Input:** root =[]
26+
27+
**Output:** 0
28+
29+
**Example 4:**
30+
31+
**Input:** root =[0]
32+
33+
**Output:** 1
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
38+
*`-100 <= Node.val <= 100`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
2+
# #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree
3+
# #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue
4+
# #Big_O_Time_O(N)_Space_O(H) #2023_11_23_Time_55_ms_(96.72%)_Space_211.3_MB_(37.23%)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode
8+
# attr_accessor :val, :left, :right
9+
# def initialize(val = 0, left = nil, right = nil)
10+
# @val = val
11+
# @left = left
12+
# @right = right
13+
# end
14+
# end
15+
# @param {TreeNode} root
16+
# @return {Integer}
17+
defmax_depth(root)
18+
find_depth(root,0)
19+
end
20+
21+
private
22+
23+
deffind_depth(node,current_depth)
24+
return0ifnode.nil?
25+
26+
current_depth +=1
27+
1 +[find_depth(node.left,current_depth),find_depth(node.right,current_depth)].max
28+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp