question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
valid-phone-numbers
Bash cat grep
bash-cat-grep-by-ajrs-pc3p
\n# Read from the file file.txt and output all valid phone numbers to stdout.\ncat file.txt | grep -E "(^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3,3}\\) [0-9]{3}-
ajrs
NORMAL
2021-07-02T05:20:01.996842+00:00
2021-07-02T05:20:01.996879+00:00
906
false
```\n# Read from the file file.txt and output all valid phone numbers to stdout.\ncat file.txt | grep -E "(^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3,3}\\) [0-9]{3}-[0-9]{4}$)"\n```
2
0
[]
2
valid-phone-numbers
this solution can not be more uglier :( but hey at least it works : D
this-solution-can-not-be-more-uglier-but-j6li
\tpublic static boolean isValid(String num){\n\t\t\tnum.trim();\n\t\t\tString[] tokens0 = num.split(" +");\n\t\t\tif (tokens0.length == 3) {\n\t\t\t\treturn fal
jerryJv11
NORMAL
2020-11-27T03:35:42.332222+00:00
2020-11-27T03:35:42.332256+00:00
595
false
\tpublic static boolean isValid(String num){\n\t\t\tnum.trim();\n\t\t\tString[] tokens0 = num.split(" +");\n\t\t\tif (tokens0.length == 3) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tString[] tokens = num.split("\\\\-" );\n\t\t\tif (tokens.length == 3) {\n\t\t\t\tif (tokens[0].length() == 3 && tokens[1].length() == 3 && t...
2
0
[]
0
valid-phone-numbers
Grep -E approach simplified
grep-e-approach-simplified-by-spenz-kru5
\ngrep -E \'^([0-9]{3}\\-|\\([0-9]{3}\\) )[0-9]{3}\\-[0-9]{4}$\' file.txt\n
spenz
NORMAL
2020-05-08T06:21:58.559327+00:00
2020-05-08T06:21:58.559381+00:00
639
false
```\ngrep -E \'^([0-9]{3}\\-|\\([0-9]{3}\\) )[0-9]{3}\\-[0-9]{4}$\' file.txt\n```
2
0
[]
0
valid-phone-numbers
grep -e solution is pretty straightforward to me, why doesn't this work? Any ideas?
grep-e-solution-is-pretty-straightforwar-srq9
\ngrep -e "\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d" -e "(\\d\\d\\d) \\d\\d\\d-\\d\\d\\d\\d"\n\n
justxn
NORMAL
2019-07-28T06:55:58.441845+00:00
2019-07-28T06:55:58.441879+00:00
571
false
```\ngrep -e "\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d" -e "(\\d\\d\\d) \\d\\d\\d-\\d\\d\\d\\d"\n```\n
2
0
[]
1
valid-phone-numbers
grep -e solution
grep-e-solution-by-lei8c8yahoo-yivs
grep -e \'^[0-9]\\{3\\}-[0-9]\\{3\\}-[0-9]\\{4\\}$\' -e \'^([0-9]\\{3\\}) [0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt
lei8c8yahoo
NORMAL
2019-06-20T01:52:23.525375+00:00
2019-06-20T01:52:23.525422+00:00
736
false
```grep -e \'^[0-9]\\{3\\}-[0-9]\\{3\\}-[0-9]\\{4\\}$\' -e \'^([0-9]\\{3\\}) [0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt```
2
0
[]
0
valid-phone-numbers
Using Bash regex
using-bash-regex-by-oxota-ro01
\n#!/usr/bin/env bash\n\nwhile read line; do\n if [[ "$line" =~ ^((\\([0-9]{3}\\) )|[0-9]{3}-)[0-9]{3}-[0-9]{4}$ ]]; then\n echo $line\n fi\ndone <
oxota
NORMAL
2019-06-07T05:45:35.421290+00:00
2019-06-07T05:45:35.421321+00:00
822
false
```\n#!/usr/bin/env bash\n\nwhile read line; do\n if [[ "$line" =~ ^((\\([0-9]{3}\\) )|[0-9]{3}-)[0-9]{3}-[0-9]{4}$ ]]; then\n echo $line\n fi\ndone < "file.txt"\n```
2
0
[]
0
valid-phone-numbers
Why is `grep -E '^\(\d{3}\) \d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$' file.txt ` wrong?
why-is-grep-e-d3-d3-d4d3-d3-d4-filetxt-w-83an
\ngrep -E \'^\\(\\d{3}\\) \\d{3}-\\d{4}$|^\\d{3}-\\d{3}-\\d{4}$\' file.txt\n\n\nError message:\n\n\nInput:\n123-456-7891\nExpected:\n123-456-7891\n\n\n\nHowever
tylerlong
NORMAL
2018-05-29T08:53:41.960391+00:00
2018-05-29T08:53:41.960391+00:00
565
false
```\ngrep -E \'^\\(\\d{3}\\) \\d{3}-\\d{4}$|^\\d{3}-\\d{3}-\\d{4}$\' file.txt\n```\n\nError message:\n\n```\nInput:\n123-456-7891\nExpected:\n123-456-7891\n```\n\n\nHowever, the following works:\n\n```\ngrep -E \'^\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$\' file.txt\n```\n\nThe only difference is...
2
0
[]
3
valid-phone-numbers
Filter Valid Phone Numbers Using grep and Regular Expressions
filter-valid-phone-numbers-using-grep-an-nyss
IntuitionThe problem is essentially about filtering lines that match specific phone number formats. Since the input is a plain text file and the pattern is well
suhrob_kalandarov
NORMAL
2025-04-10T03:44:55.887697+00:00
2025-04-10T03:52:54.938301+00:00
38
false
# Intuition The problem is essentially about filtering lines that match specific phone number formats. Since the input is a plain text file and the pattern is well-defined, a regular expression is the most efficient tool to recognize valid formats. # Approach We use the grep command with the -E flag (extended regular ...
1
0
['Bash']
1
valid-phone-numbers
easy bash solution to understand
easy-bash-solution-to-understand-by-prat-wr9v
IntuitionApproachComplexity Time complexity: Space complexity: Code
pratik5722
NORMAL
2025-03-03T07:57:49.434122+00:00
2025-03-03T07:57:49.434122+00:00
450
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
1
0
['Bash']
0
valid-phone-numbers
Easy Solution Beats 98.24%
easy-solution-beats-9824-by-rishabh_234-02q1
PLEASE UPVOTE\n# FEEL FREE TO ASK\n# Code\nbash []\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep \'^\\([0-9]\\{3\\}-\\|([0-
r_90
NORMAL
2024-10-20T16:52:04.132218+00:00
2024-10-20T16:52:04.132242+00:00
21
false
# PLEASE UPVOTE\n# FEEL FREE TO ASK\n# Code\n```bash []\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}\n``` file.txt\n```
1
1
['Shell', 'Bash']
0
valid-phone-numbers
USING GREP COMMAND
using-grep-command-by-ujjwalvarma6948-kvfl
//PLEASE UPVOTE IF YOU LIKE THE APPRAOCH !!\n\n# Code\n\ngrep -e "^[0-9]\\{3\\}\\-[0-9]\\{3\\}\\-[0-9]\\{4\\}$" -e "^([0-9]\\{3\\}) [0-9]\\{3\\}\\-[0-9]\\{4\\}$
ujjwalvarma6948
NORMAL
2024-03-06T16:57:45.416155+00:00
2024-03-06T16:57:45.416187+00:00
553
false
//PLEASE UPVOTE IF YOU LIKE THE APPRAOCH !!\n\n# Code\n```\ngrep -e "^[0-9]\\{3\\}\\-[0-9]\\{3\\}\\-[0-9]\\{4\\}$" -e "^([0-9]\\{3\\}) [0-9]\\{3\\}\\-[0-9]\\{4\\}$" file.txt\n```
1
0
['Bash']
0
valid-phone-numbers
Number Validate
number-validate-by-abdulvoris-w1ja
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Abdulvoris
NORMAL
2024-01-10T19:20:25.190869+00:00
2024-01-10T19:20:25.190913+00:00
1,309
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Shell', 'Number Theory', 'Bash']
1
valid-phone-numbers
Solution
solution-by-suraj_singh_rajput-b8qe
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Suraj_singh_rajput
NORMAL
2023-12-22T07:11:38.010009+00:00
2023-12-22T07:11:38.010039+00:00
15
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Bash']
0
valid-phone-numbers
Easy and simple :0 WOW (0_0)
easy-and-simple-0-wow-0_0-by-azamatabduv-deli
\n# Code\n\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep -E \'^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}\n file.txt\n```
AzamatAbduvohidov
NORMAL
2023-05-13T13:06:27.535847+00:00
2023-05-13T13:06:27.535877+00:00
6,992
false
\n# Code\n```\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep -E \'^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}\n``` file.txt\n```\nThis script uses grep command with extended regular expressions (-E) to match the pattern of valid phone numbers. The pattern matches either (xxx) xxx-x...
1
0
['Bash']
1
valid-phone-numbers
[BASH] using while and if statements
bash-using-while-and-if-statements-by-am-icko
\n#!/bin/bash\nfile_name="file.txt"\nphone_number_regx=\'(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)|(^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$)\'\n\nwhile read line;\ndo\n\tif [[
amresh564
NORMAL
2022-11-30T19:16:26.272570+00:00
2022-12-16T15:37:59.416202+00:00
136
false
```\n#!/bin/bash\nfile_name="file.txt"\nphone_number_regx=\'(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)|(^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$)\'\n\nwhile read line;\ndo\n\tif [[ "$line" =~ $phone_number_regx ]]; then\n\t\techo $line\n\tfi\ndone<$file_name\n```
1
0
['Bash']
0
valid-phone-numbers
Simple Solution with grep - E with | (OR) | detail explanation
simple-solution-with-grep-e-with-or-deta-nuov
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com\nIt is very useful, and I just wanted to shar
pmistry_
NORMAL
2022-09-03T14:59:10.745206+00:00
2022-10-12T15:06:32.927809+00:00
684
false
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com\nIt is very useful, and I just wanted to share it with you.\nNote: You can bookmark it as a resource, and for another approaches\n<br>\n\n```\ngrep -E \'(^[0-9]{3}-|^\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$\' fi...
1
0
[]
0
valid-phone-numbers
[Regex] Runtime: 649 ms, faster than 5.46% of Go submissions
regex-runtime-649-ms-faster-than-546-of-cry2s
Complexity Analysis\n Time: O(N), where N is the number of lines within the file.txt.\n Space: O(1), constant number of variables used.\n\nsh\nfunction isValid(
d8dca782
NORMAL
2022-07-18T00:34:59.821033+00:00
2022-07-18T00:34:59.821068+00:00
384
false
**Complexity Analysis**\n* Time: O(N), where N is the number of lines within the file.txt.\n* Space: O(1), constant number of variables used.\n\n```sh\nfunction isValid()\n{\n if [[ $1 =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then\n return $(true)\n fi\n if [[ $1 =~ ^[(][0-9]{3}[)][[:space:]][0-9]{3}[-][0-9]{4}$ ]]; t...
1
0
[]
0
valid-phone-numbers
One-Line Solution With Regex
one-line-solution-with-regex-by-jacdabro-uinm
Here is my solution:\n\n\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep -E \'(^[0-9]{3}-|^\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$
jacdabro
NORMAL
2022-07-05T15:05:39.688161+00:00
2022-07-05T16:13:24.440144+00:00
398
false
Here is my solution:\n\n```\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep -E \'(^[0-9]{3}-|^\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$\' file.txt\n```\n\nor \n\n```\ngrep -P \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n```
1
0
[]
1
valid-phone-numbers
[BASH] - Easy One Liner
bash-easy-one-liner-by-tursunboyevjahong-do0u
https://leetcode.com/problems/valid-phone-numbers/discuss/843424/BASH-Easy-One-Liner\n\n\n1) grep -P \'^(\\d{3}-\\d{3}-\\d{4}|\\(\\d{3}\\) \\d{3}-\\d{4})$\' fil
TursunboyevJahongir
NORMAL
2022-06-05T05:03:13.213097+00:00
2022-06-05T05:06:47.777270+00:00
847
false
https://leetcode.com/problems/valid-phone-numbers/discuss/843424/BASH-Easy-One-Liner\n\n```\n1) grep -P \'^(\\d{3}-\\d{3}-\\d{4}|\\(\\d{3}\\) \\d{3}-\\d{4})$\' file.txt\n```\n\n```\n2) grep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n```\n\n```\n3) egrep \'^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\)\\s[0-...
1
0
[]
0
valid-phone-numbers
Grep [Runtime: 4 ms Memory Usage: 3.1 MB]
grep-runtime-4-ms-memory-usage-31-mb-by-3ock2
\ngrep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n
ankitkumar734ac
NORMAL
2022-01-23T13:46:28.992062+00:00
2022-01-23T13:46:28.992106+00:00
440
false
```\ngrep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n```
1
0
[]
0
valid-phone-numbers
0ms solution. 100% faster!
0ms-solution-100-faster-by-akshayabee19-dupq
grep -P "^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$" file.txt
akshayabee19
NORMAL
2021-08-19T17:05:41.471313+00:00
2021-08-19T17:05:41.471359+00:00
1,015
false
```grep -P "^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$" file.txt```
1
0
[]
0
valid-phone-numbers
egrep solution with cat
egrep-solution-with-cat-by-letrounet-uasi
\ncat file.txt | egrep "^((\\([0-9]{3}\\) )|([0-9]{3}-))[0-9]{3}-{0,1}[0-9]{4}$"\n\n
letrounet
NORMAL
2021-07-20T00:53:07.820027+00:00
2021-07-20T00:53:07.820165+00:00
556
false
```\ncat file.txt | egrep "^((\\([0-9]{3}\\) )|([0-9]{3}-))[0-9]{3}-{0,1}[0-9]{4}$"\n```\n
1
0
[]
1
valid-phone-numbers
Egrep Solution
egrep-solution-by-souradeepsaha-zkn5
egrep "^([0-9]{3}-|\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$" file.txt
souradeepsaha
NORMAL
2021-06-11T05:42:35.727198+00:00
2021-06-11T05:42:35.727233+00:00
365
false
```egrep "^([0-9]{3}-|\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$" file.txt```
1
1
[]
0
valid-phone-numbers
No grep solution (actually works)
no-grep-solution-actually-works-by-maker-7q7c
It took me a while, as some attempts "work on my machine" but not here lol. I\'m sure this can be simplified, but it took me a few hours to get this to work, so
makerbro
NORMAL
2021-05-29T17:33:40.815417+00:00
2021-05-29T17:35:29.616511+00:00
626
false
It took me a while, as some attempts "work on my machine" but not here lol. I\'m sure this can be simplified, but it took me a few hours to get this to work, so will stop here and see if anyone out there improves upon it. Using a slightly shorter regex, and even looking at SO examples, people seem to not care about a n...
1
0
[]
1
valid-phone-numbers
Quick grep solution
quick-grep-solution-by-lozuwaucb-r9pb
Check the number lengths, the tokens and make sure it starts and ends appropiately. \n\n\ncat file.txt | grep -Ei "^(\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$)|^([0-9
lozuwaucb
NORMAL
2021-04-18T01:18:47.468668+00:00
2021-04-18T01:18:47.468699+00:00
773
false
Check the number lengths, the tokens and make sure it starts and ends appropiately. \n\n```\ncat file.txt | grep -Ei "^(\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$)|^([0-9]{3}-[0-9]{3}-[0-9]{4}$)"\n```
1
1
[]
1
valid-phone-numbers
grep - P
grep-p-by-superroshan-22d7
grep -P "^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$" file.txt
superroshan
NORMAL
2021-01-21T05:18:45.605661+00:00
2021-01-21T05:19:44.072512+00:00
721
false
```grep -P "^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$" file.txt```
1
0
[]
0
valid-phone-numbers
Query related to input
query-related-to-input-by-_pii-cs5c
Can anyone tell me why this input is not valid \n(001)-345-0000\n\nthis is my bash one liner\ngrep -P \'^(\\(?\\d{3}|\\d{3})\\)?[\\s|-]\\d{3}-\\d{4}$\' file.txt
_pii_
NORMAL
2020-06-24T18:27:03.699601+00:00
2020-06-24T18:27:03.699645+00:00
355
false
Can anyone tell me why this input is not valid \n```(001)-345-0000```\n\nthis is my bash one liner\n```grep -P \'^(\\(?\\d{3}|\\d{3})\\)?[\\s|-]\\d{3}-\\d{4}$\' file.txt```\n\nit also accepts the above number but gets an error saying it should not get accepted\n
1
0
[]
1
valid-phone-numbers
grep -E
grep-e-by-gprx100-tjbr
```\ncat file.txt | grep -E "^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$"\n\n
gprx100
NORMAL
2020-04-12T21:49:03.574670+00:00
2020-04-12T21:49:03.574709+00:00
592
false
```\ncat file.txt | grep -E "^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$"\n\n
1
0
[]
0
valid-phone-numbers
mac bash or linux bash?
mac-bash-or-linux-bash-by-theosoft-ro89
At first, I use this code:\n\ncat file.txt | grep -E "^(\\d{3}-|\\(\\d{3}\\)\\ )\\d{3}-\\d{4}$"\n\nIt works in my mac bash shell, but failed for the submission.
theosoft
NORMAL
2020-03-27T13:45:38.099190+00:00
2020-03-27T13:45:38.099225+00:00
435
false
At first, I use this code:\n```\ncat file.txt | grep -E "^(\\d{3}-|\\(\\d{3}\\)\\ )\\d{3}-\\d{4}$"\n```\nIt works in my mac bash shell, but failed for the submission.\nThen I changed my code to below:\n```\ncat file.txt | grep -E "^([0-9]{3}-|\\([0-9]{3}\\)\\ )[0-9]{3}-[0-9]{4}$"\n```\nThen passed...
1
0
[]
1
valid-phone-numbers
why shebang and regular bash script not working?
why-shebang-and-regular-bash-script-not-jn15n
\n#!/bin/bash\ninput = \'file.txt\'\nre=\'([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]\'\nre2=\'[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]\'
erjan
NORMAL
2020-03-18T11:08:22.584823+00:00
2020-03-18T11:08:22.584858+00:00
225
false
```\n#!/bin/bash\ninput = \'file.txt\'\nre=\'([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]\'\nre2=\'[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]\'\nwhile IFS read -r line;\ndo if [[$line = ~ $re || $line = ~re2 ]];\nthen echo $line\n```
1
1
[]
0
valid-phone-numbers
Solution without grep, awk, sed
solution-without-grep-awk-sed-by-kostmet-ncnp
\nwhile read LINE; do if [[ $LINE =~ (^\\([0-9]{3}\\)[[:blank:]][0-9]{3}-[0-9]{4}$)|(^[0-9]{3}-[0-9]{3}-[0-9]{4}$) ]]; then echo $LINE; fi; done < file.txt\n
kostmetallist
NORMAL
2019-11-21T23:54:52.516598+00:00
2019-11-21T23:54:52.516642+00:00
920
false
```\nwhile read LINE; do if [[ $LINE =~ (^\\([0-9]{3}\\)[[:blank:]][0-9]{3}-[0-9]{4}$)|(^[0-9]{3}-[0-9]{3}-[0-9]{4}$) ]]; then echo $LINE; fi; done < file.txt\n```
1
0
[]
0
valid-phone-numbers
Trivial awk solution (probably been posted before)
trivial-awk-solution-probably-been-poste-y70g
\nawk -e \'/^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$/{ print; }\' file.txt;\n
thebel
NORMAL
2019-11-14T23:45:15.447901+00:00
2019-11-14T23:45:15.447952+00:00
321
false
```\nawk -e \'/^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$/{ print; }\' file.txt;\n```
1
0
[]
3
valid-phone-numbers
Time = 0ms beats 100% with Memory = 3.1 MB, simple using Regex Grep
time-0ms-beats-100-with-memory-31-mb-sim-w8wg
\ncat file.txt | grep -E "(^[0-9]{3}-|^\\([0-9]{3}\\)\\s)[0-9]{3}-[0-9]{4}$"\n
terminator1296
NORMAL
2019-10-07T09:49:00.767231+00:00
2019-10-07T09:49:00.767278+00:00
828
false
```\ncat file.txt | grep -E "(^[0-9]{3}-|^\\([0-9]{3}\\)\\s)[0-9]{3}-[0-9]{4}$"\n```
1
0
[]
0
valid-phone-numbers
grep -P solution using conditional regex with explanation
grep-p-solution-using-conditional-regex-6tlnl
Conditional regular expressions are not always the most efficient solution. However, they can be really useful in reducing the length of the regular expression
akpircher
NORMAL
2019-07-09T20:22:49.803994+00:00
2019-07-09T20:22:49.804032+00:00
537
false
Conditional regular expressions are not always the most efficient solution. However, they _can_ be really useful in reducing the length of the regular expression by removing a long "or" statement with repeating elements. This is not one of those cases, but it\'s a fun solution. The downside is that they are harder to r...
1
0
[]
0
valid-phone-numbers
egrep solution
egrep-solution-by-charliekenney23-ee73
\ncat file.txt | egrep \'^(\\([[:digit:]]{3}\\) |[[:digit:]]{3}-)[[:digit:]]{3}-[[:digit:]]{4}$\'\n
charliekenney23
NORMAL
2019-06-25T05:38:48.434891+00:00
2019-06-25T05:38:48.434936+00:00
471
false
```\ncat file.txt | egrep \'^(\\([[:digit:]]{3}\\) |[[:digit:]]{3}-)[[:digit:]]{3}-[[:digit:]]{4}$\'\n```
1
0
[]
0
valid-phone-numbers
egrep solution
egrep-solution-by-codeywodey-7t05
bash\ncat file.txt | egrep \'^(\\([0-9]{3}\\)\\s{1}|[0-9]{3}-)[0-9]{3}-[0-9]{4}$\'\n
codeywodey
NORMAL
2019-04-14T01:18:40.511719+00:00
2019-04-14T01:18:40.511762+00:00
332
false
```bash\ncat file.txt | egrep \'^(\\([0-9]{3}\\)\\s{1}|[0-9]{3}-)[0-9]{3}-[0-9]{4}$\'\n```
1
0
[]
1
valid-phone-numbers
Fastest Awk Solution [8ms]
fastest-awk-solution-8ms-by-ganbatteimas-ehga
awk\nawk \'{if($0 ~ /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/ || $0 ~ /^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$/){printf $0"\\n"}}\' file.txt\n\n\nSimply processes each line and
ganbatteimasu19
NORMAL
2019-03-25T00:50:51.405276+00:00
2019-03-25T00:50:51.405368+00:00
428
false
```awk\nawk \'{if($0 ~ /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/ || $0 ~ /^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$/){printf $0"\\n"}}\' file.txt\n```\n\nSimply processes each line and checks if the entire record (default newline separated) has exactly the needed formats specified and prints each line that does.
1
0
[]
0
valid-phone-numbers
Grep
grep-by-cantu_reinhard-0p20
Never use solutions like these. Debugging this would be hell. grep "^\(\([0-9]\{3\}-\)\|\(([0-9]\{3\})\s\)\)[0-9]\{3\}-[0-9]\{4\}$" file.txt
cantu_reinhard
NORMAL
2018-10-16T21:07:42.070900+00:00
2018-10-16T21:07:42.070993+00:00
386
false
Never use solutions like these. Debugging this would be hell. ``` grep "^\(\([0-9]\{3\}-\)\|\(([0-9]\{3\})\s\)\)[0-9]\{3\}-[0-9]\{4\}$" file.txt ```
1
0
[]
0
valid-phone-numbers
Wrong Output of Shell question "valid phone number"
wrong-output-of-shell-question-valid-pho-s0ur
For Shell Question "valid phone number", I submitted the below code: \n\n while read LINE\n do\n if [[ $LINE =~ "^\([0-9]{3}\) [0-9]{3}-[0-9]{4}
bati_goal
NORMAL
2015-04-23T12:58:41+00:00
2015-04-23T12:58:41+00:00
595
false
For Shell Question "valid phone number", I submitted the below code: \n\n while read LINE\n do\n if [[ $LINE =~ "^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$" ]] || [[ $LINE =~ "^[0-9]{3}-[0-9]{3}-[0-9]{4}$" ]]; then\n echo $LINE\n fi\n done < 'file.txt'\n\nFinally, Leetcode judge my code ...
1
0
[]
1
valid-phone-numbers
whereis wrong with my pattern? ask for help
whereis-wrong-with-my-pattern-ask-for-he-2rh6
my grep solution, the code is like:\n\ngrep "\\(?\\d{3}\\)?[- ]\\d{3}-\\d{4}" file.txt\n\nbut when it came across test case "123-456-7891", it didn't print out
lucasdove
NORMAL
2017-05-21T12:30:41.550000+00:00
2017-05-21T12:30:41.550000+00:00
435
false
my grep solution, the code is like:\n```\ngrep "\\(?\\d{3}\\)?[- ]\\d{3}-\\d{4}" file.txt\n```\nbut when it came across test case "123-456-7891", it didn't print out anything. could someone tell me why please?\nYet I put test case "123-456-7891" into a file, and open it with notepad++ on windows, using the same pattern...
1
0
[]
1
valid-phone-numbers
📞 Master Regex Filtering – Unlock the Power of Pattern Matching in Bash!
master-regex-filtering-unlock-the-power-ggz6v
IntuitionWhen processing text files, regular expressions allow us to find lines that match specific patterns. In this case, we're asked to identify phone number
loginov-kirill
NORMAL
2025-04-07T22:12:54.357568+00:00
2025-04-07T22:12:54.357568+00:00
433
false
# Intuition When processing text files, regular expressions allow us to find lines that match specific patterns. In this case, we're asked to identify phone numbers in two formats. # Approach ![image.png](https://assets.leetcode.com/users/images/e6619933-3be7-471b-85ec-2b91edba9e10_1744063971.0943432.png) - Use `gre...
0
0
['Shell', 'Bash']
1
valid-phone-numbers
Kian Ilanluo
kian-ilanluo-by-kianilanluo-r3ry
IntuitionJust Think simpleApproach60 ms Beats 84.45%Complexity Time complexity: 60 ms Beats 84.45% Space complexity: 60 ms Beats 84.45% Code
kianilanluo
NORMAL
2025-03-31T14:31:38.792726+00:00
2025-03-31T14:31:38.792726+00:00
2
false
# Intuition Just Think simple # Approach 60 ms Beats 84.45% # Complexity - Time complexity: 60 ms Beats 84.45% - Space complexity: 60 ms Beats 84.45% # Code ```bash [] # Read from the file file.txt and output all valid phone numbers to stdout. grep -E '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4} ``` ...
0
0
['Bash']
0
valid-phone-numbers
shell
shell-by-rohan_shelke-9azd
IntuitionApproachComplexity Time complexity: Space complexity: Code
Rohan_Shelke
NORMAL
2025-03-30T05:27:00.605218+00:00
2025-03-30T05:27:00.605218+00:00
4
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Bash']
0
valid-phone-numbers
❤️ Well explained what is the regex
well-explained-what-is-the-regex-by-muna-ksz9
IntuitionApproachComplexity Time complexity: Space complexity: CodeCode explaingrep Search for text in a file -E Enable extended regex ^ Match start of line $
Munavar_PM
NORMAL
2025-03-28T21:55:14.458720+00:00
2025-03-28T21:55:14.458720+00:00
4
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ...
0
0
['Bash']
0
valid-phone-numbers
One liner command
one-liner-command-by-niladridhar1-5jls
IntuitionSimple One liner command to get all the valid phone numbersCode
niladridhar1
NORMAL
2025-03-27T05:04:01.867928+00:00
2025-03-27T05:04:01.867928+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Simple One liner command to get all the valid phone numbers # Code ```bash [] # Read from the file file.txt and output all valid phone numbers to stdout. cat file.txt | grep -E '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4} ...
0
0
['Bash']
0
valid-phone-numbers
Solution using grep onliner extended regex
solution-using-grep-onliner-extended-reg-1y6y
Code
Nirzak
NORMAL
2025-03-25T19:50:40.323345+00:00
2025-03-25T19:50:40.323345+00:00
4
false
# Code ```bash [] # Read from the file file.txt and output all valid phone numbers to stdout. grep -E "^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$" file.txt ```
0
0
['Bash']
0
valid-phone-numbers
One-line solution with regex pattern
one-line-solution-with-regex-pattern-by-c2pdq
IntuitionApproach Use regex as pattern for grep Either start with any of these 2 patterns => ^(pattern1|pattern2) pattern1: 3 digits and dash (e.g. 123-) =>
Lesliee_2101
NORMAL
2025-03-21T13:40:09.202350+00:00
2025-03-21T13:41:27.490328+00:00
8
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> - Use regex as pattern for grep - Either start with any of these 2 patterns => ^($$pattern1$$|$$pattern2$$) $$pattern1$$: 3 digits and dash (e.g. 123-) => \d{3}- $$patter...
0
0
['Bash']
0
valid-phone-numbers
Bash code 100%
bash-code-100-by-sunnat_dev_2011-ycdx
IntuitionApproachComplexity Time complexity: Space complexity: Code
sunnat_dev_2011
NORMAL
2025-03-20T17:55:19.179327+00:00
2025-03-20T17:55:19.179327+00:00
9
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Bash']
0
valid-phone-numbers
Simplest approach with explanation of regex and flags
simplest-approach-with-explanation-of-re-7s6v
IntuitionJust use grep command with -E flag for multiple regexApproachpass the regular expressions [] : with - between represents range of selection {} : repres
Abhijit_Dolai
NORMAL
2025-03-19T20:07:21.790181+00:00
2025-03-19T20:07:21.790181+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Just use grep command with -E flag for multiple regex # Approach <!-- Describe your approach to solving the problem. --> pass the regular expressions - [] : with - between represents range of selection - {} : represents no. of times - \ :...
0
0
['Bash']
0
valid-phone-numbers
Regex Phonenumbers
regex-phonenumbers-by-mrsander901-s7vo
IntuitionUse regex to filter phone numbers from a file.ApproachUse grep -E with two patterns: xxx-xxx-xxxx or (xxx) xxx-xxxx, anchored to match full lines.Code
MrSander901
NORMAL
2025-03-09T15:37:10.973276+00:00
2025-03-09T15:37:10.973276+00:00
9
false
# Intuition Use regex to filter phone numbers from a file. # Approach Use grep -E with two patterns: xxx-xxx-xxxx or (xxx) xxx-xxxx, anchored to match full lines. # Code ```bash [] # Read from the file file.txt and output all valid phone numbers to stdout. grep -E '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-...
0
0
['Bash']
0
valid-phone-numbers
193. Valid Phone Numbers Solution
193-valid-phone-numbers-solution-by-muha-quch
IntuitionThe problem requires extracting phone numbers that match two specific formats: (xxx) xxx-xxxx (where x is a digit) xxx-xxx-xxxx Given that the input is
Muhammad_Haris_Ahsan
NORMAL
2025-03-06T02:42:41.210800+00:00
2025-03-06T02:42:41.210800+00:00
12
false
# Intuition The problem requires extracting phone numbers that match two specific formats: 1. `(xxx) xxx-xxxx` (where `x` is a digit) 2. `xxx-xxx-xxxx` Given that the input is a text file where each line contains a single phone number, we can efficiently solve this using `grep`, a command-line tool that supports regu...
0
0
['Bash']
0
valid-phone-numbers
Concise Shell Script for Valid Phone Number Extraction using grep
concise-shell-script-for-valid-phone-num-dbev
IntuitionUse grep to filter lines in file.txt that match the specified phone number formats.ApproachUse grep -E with a regular expression to match either (xxx)
sharipovikromjon
NORMAL
2025-03-01T01:50:04.140774+00:00
2025-03-01T01:50:04.140774+00:00
7
false
# Intuition Use **grep** to filter lines in **file.txt** that match the specified phone number formats. # Approach Use **grep -E** with a regular expression to match either **(xxx) xxx-xxxx** or **xxx-xxx-xxxx** formats. Anchor the regex to the start (**^**) and end (**$**) of the line for exact matches. Use **\s** for...
0
0
['Bash']
0
valid-phone-numbers
193. Valid Phone Numbers
193-valid-phone-numbers-by-1hbxnllboo-odf8
IntuitionUse regex to match valid phone numbers.ApproachUsing grep is enough to perform the regex match.Code
1hBxNLlbOO
NORMAL
2025-02-16T05:42:16.589322+00:00
2025-02-16T05:42:16.589322+00:00
9
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Use regex to match valid phone numbers. # Approach <!-- Describe your approach to solving the problem. --> Using `grep` is enough to perform the regex match. # Code ```bash [] # Read from the file file.txt and output all valid phone numbe...
0
0
['Bash']
0
valid-phone-numbers
No brainer, one liner
no-brainer-one-liner-by-nishant_sheoran-nhmr
Code
nishant_sheoran
NORMAL
2025-02-14T12:44:15.040346+00:00
2025-02-14T12:44:15.040346+00:00
20
false
# Code ```bash [] grep -e "^[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$" -e "^([0-9]\{3\}) [0-9]\{3\}\-[0-9]\{4\}$" file.txt ```
0
0
['Shell', 'Bash']
0
valid-phone-numbers
Valid Phone Number using GREP
valid-phone-number-using-grep-by-samahaa-7w9u
IntuitionThe problem requires us to extract valid phone numbers from a file. A valid phone number follows one of two formats:(xxx) xxx-xxxx xxx-xxx-xxxx Given t
samahaabbas
NORMAL
2025-02-09T11:49:03.900646+00:00
2025-02-09T11:49:03.900646+00:00
14
false
# Intuition The problem requires us to extract valid phone numbers from a file. A valid phone number follows one of two formats: (xxx) xxx-xxxx xxx-xxx-xxxx Given that each line contains only one phone number, a line-by-line pattern match is the best approach. # Approach We use grep with extended regular expressions ...
0
0
['Bash']
0
valid-phone-numbers
Solution using grep+regex
solution-using-grepregex-by-u0ejuzk0o7-0ks8
IntuitionThe problem asks us to identify valid phone numbers from a file. We are given specific formats that a valid phone number must follow: (xxx) xxx-xxxx xx
mehraj_hossain
NORMAL
2025-02-09T02:35:01.509985+00:00
2025-02-09T02:35:01.509985+00:00
12
false
# Intuition The problem asks us to identify valid phone numbers from a file. We are given specific formats that a valid phone number must follow: - `(xxx) xxx-xxxx` - `xxx-xxx-xxxx` Since both formats are well-structured, regular expressions (regex) can easily be used to capture and validate these patterns. We can us...
0
0
['Bash']
0
valid-phone-numbers
Valid Phone Numbers - Solution
valid-phone-numbers-solution-by-4auz20ig-cx9v
IntuitionThe problem requires extracting and printing phone numbers that match a specific format from a text file. Since each line contains a single phone numbe
4AUZ20iGg6
NORMAL
2025-02-08T22:02:03.666894+00:00
2025-02-08T22:02:03.666894+00:00
7
false
# Intuition The problem requires extracting and printing phone numbers that match a specific format from a text file. Since each line contains a single phone number, we need a method to validate each line against the given formats: 1. ```(xxx) xxx-xxxx``` (Parentheses and space included) 1. ```xxx-xxx-xxxx``` (Only da...
0
0
['Bash']
0
valid-phone-numbers
Valid Phone Number
valid-phone-number-by-suman_tandan-6xjs
IntuitionApproachComplexity Time complexity: Space complexity: Code
suman_tandan
NORMAL
2025-02-05T05:30:25.108829+00:00
2025-02-05T05:30:25.108829+00:00
12
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Bash']
0
valid-phone-numbers
Bash | Validate Phone Numbers Using grep
bash-validate-phone-numbers-using-grep-b-tyx5
IntuitionWe need to extract valid phone numbers from file.txt. A valid phone number follows one of these two formats: xxx-xxx-xxxx (xxx) xxx-xxxx Since we are w
SamuelBrhaneAlemayohu
NORMAL
2025-02-03T17:40:00.176296+00:00
2025-02-03T17:40:00.176296+00:00
32
false
# Intuition We need to extract valid phone numbers from `file.txt`. A **valid phone number** follows one of these two formats: 1. `xxx-xxx-xxxx` 2. `(xxx) xxx-xxxx` Since we are working in **Bash**, we can use `grep` with **regular expressions** to filter out invalid numbers. # Approach 1. **Use `grep -E` (Extended...
0
0
['Bash']
0
valid-phone-numbers
193. Valid Phone Numbers
193-valid-phone-numbers-by-g8xd0qpqty-s1zv
IntuitionApproachComplexity Time complexity: Space complexity: Code
G8xd0QPqTy
NORMAL
2025-01-10T07:21:01.725681+00:00
2025-01-10T07:21:01.725681+00:00
24
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Bash']
0
valid-phone-numbers
POSIX Regexps
posix-regexps-by-spektrokalter-7u2l
null
spektrokalter
NORMAL
2025-01-09T09:04:05.393848+00:00
2025-01-09T09:04:05.393848+00:00
14
false
```txt cat file.txt |grep '^\(([0-9][0-9][0-9]) \|[0-9][0-9][0-9]-\)[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] ``` ```
0
0
['Bash']
0
valid-phone-numbers
Sonakshi Tiwari
sonakshi-tiwari-by-sonakshigtiwari-ep9k
IntuitionApproachComplexity Time complexity: Space complexity: Code
SONAKSHIGTIWARI
NORMAL
2025-01-07T13:40:19.957434+00:00
2025-01-07T13:40:19.957434+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Bash']
0
binary-tree-zigzag-level-order-traversal
[c++] 5ms version: one queue and without reverse operation by using size of each level
c-5ms-version-one-queue-and-without-reve-8msj
\nAssuming after traversing the 1st level, nodes in queue are {9, 20, 8}, And we are going to traverse 2nd level, which is even line and should print value from
stevencooks
NORMAL
2015-03-13T20:50:25+00:00
2018-10-19T14:45:07.857245+00:00
80,421
false
\nAssuming after traversing the 1st level, nodes in queue are {9, 20, 8}, And we are going to traverse 2nd level, which is even line and should print value from right to left [8, 20, 9]. \n\nWe know there are 3 nodes in current queue, so the vector for this level in final result should be of size 3. \nThen, queue...
503
8
['C++']
47
binary-tree-zigzag-level-order-traversal
My accepted JAVA solution
my-accepted-java-solution-by-awaylu-dnra
public class Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) \n {\n List<List<Integer>> sol = new ArrayList<>()
awaylu
NORMAL
2014-09-17T06:17:28+00:00
2018-10-20T04:13:36.538433+00:00
145,645
false
public class Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) \n {\n List<List<Integer>> sol = new ArrayList<>();\n travel(root, sol, 0);\n return sol;\n }\n \n private void travel(TreeNode curr, List<List<Integer>> sol, int ...
476
7
['Java']
87
binary-tree-zigzag-level-order-traversal
JAVA Double Stack Solution
java-double-stack-solution-by-tjcd-uqg2
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n TreeNode c=root;\n List<List<Integer>> ans =new ArrayList<List<Integer>>();\n i
tjcd
NORMAL
2015-12-26T14:24:44+00:00
2018-10-26T19:00:53.166043+00:00
30,507
false
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n TreeNode c=root;\n List<List<Integer>> ans =new ArrayList<List<Integer>>();\n if(c==null) return ans;\n Stack<TreeNode> s1=new Stack<TreeNode>();\n Stack<TreeNode> s2=new Stack<TreeNode>();\n s1.push(root);\n wh...
336
4
['Java']
29
binary-tree-zigzag-level-order-traversal
[c++] Easy to understand, one approach to solve Zigzag & Level order traversal
c-easy-to-understand-one-approach-to-sol-0vig
This approch can be used to solve both level order traversal & zig zag traversal.\nIt\'s easy to start with the level order traversal. I hope the comments are c
mnumtw7
NORMAL
2018-09-26T15:49:46.925697+00:00
2019-08-17T23:52:56.083858+00:00
22,383
false
This approch can be used to solve both level order traversal & zig zag traversal.\nIt\'s easy to start with the level order traversal. I hope the comments are clear.\n\n```\nvector<vector<int>> levelOrder(TreeNode* root) {\n if (!root) return {}; // return if root is null\n queue<TreeNode*> q;\n q....
241
3
['Tree', 'C']
19
binary-tree-zigzag-level-order-traversal
[Python] Clean BFS solution, explained
python-clean-bfs-solution-explained-by-d-eudl
In this problem we need to traverse binary tree level by level. When we see levels in binary tree, we need to think about bfs, because it is its logic: it first
dbabichev
NORMAL
2020-07-22T10:47:30.078047+00:00
2020-07-22T10:47:30.078100+00:00
19,826
false
In this problem we need to traverse binary tree level by level. When we see levels in binary tree, we need to think about **bfs**, because it is its logic: it first traverse all neighbors, before we go deeper. Here we also need to change direction on each level as well. So, algorithm is the following:\n\n1. We create *...
237
5
['Breadth-First Search']
23
binary-tree-zigzag-level-order-traversal
A concise and easy understanding Java solution
a-concise-and-easy-understanding-java-so-p03m
public class Solution {\n public List> zigzagLevelOrder(TreeNode root) {\n List> res = new ArrayList<>();\n if(root == null) return res;\n\n
graceluli
NORMAL
2015-11-11T23:29:46+00:00
2018-10-10T02:00:58.630632+00:00
36,436
false
public class Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> res = new ArrayList<>();\n if(root == null) return res;\n\n Queue<TreeNode> q = new LinkedList<>();\n q.add(root);\n boolean order = true;\n int size = 1;\n\n ...
140
4
[]
23
binary-tree-zigzag-level-order-traversal
Python simple BFS
python-simple-bfs-by-fangkunjnsy-ynaq
Simple straightforward solution using flag to decide whether from left to right or from right to left\n\n class Solution(object):\n def zigzagLevelOrder(s
fangkunjnsy
NORMAL
2015-09-23T04:24:02+00:00
2018-10-06T11:09:10.913549+00:00
30,587
false
Simple straightforward solution using flag to decide whether from left to right or from right to left\n\n class Solution(object):\n def zigzagLevelOrder(self, root):\n """\n :type root: TreeNode\n :rtype: List[List[int]]\n """\n if not root: return []\n res, temp, stack, ...
105
17
[]
31
binary-tree-zigzag-level-order-traversal
Clear iterative solution with deque, no reverse
clear-iterative-solution-with-deque-no-r-rtvl
for zig, pop_back, push_front, left then right, \n\n for zag, pop_front, push_back, right then left\n\n vector> zigzagLevelOrder(TreeNode* root) {\n v
qeatzy
NORMAL
2015-08-09T08:33:26+00:00
2018-10-04T05:20:36.521379+00:00
16,834
false
for zig, pop_back, push_front, left then right, \n\n for zag, pop_front, push_back, right then left\n\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n std::deque<TreeNode*> deq;\n deq.push_back(root);\n int iszig=1;\n ...
78
0
[]
6
binary-tree-zigzag-level-order-traversal
Four python solutions
four-python-solutions-by-nth-attempt-5r7m
Approach - 1\nUsing single ended queue and reverse. Although we are using deque we are utilizing only single ended queue functionality here.\npython\ndef zigzag
nth-attempt
NORMAL
2020-07-23T02:20:54.455285+00:00
2021-02-28T16:28:49.579547+00:00
10,222
false
#### Approach - 1\nUsing single ended queue and reverse. Although we are using `deque` we are utilizing only single ended queue functionality here.\n```python\ndef zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n\tif not root: return []\n\tqueue = collections.deque([root])\n\tres = []\n\teven_level = False\...
74
0
['Queue', 'Python', 'Python3']
8
binary-tree-zigzag-level-order-traversal
8-liner Python
8-liner-python-by-tlhuang-w8ga
\nclass Solution(object):\n def zigzagLevelOrder(self, root):\n """\n :type root: TreeNode\n :rtype: List[List[int]]\n """\n
tlhuang
NORMAL
2016-09-05T21:35:46.279000+00:00
2018-10-22T20:47:37.147954+00:00
10,575
false
```\nclass Solution(object):\n def zigzagLevelOrder(self, root):\n """\n :type root: TreeNode\n :rtype: List[List[int]]\n """\n if not root:\n return []\n res, level, direction = [], [root], 1\n while level:\n res.append([n.val for n in level][::...
74
3
[]
11
binary-tree-zigzag-level-order-traversal
Two simple approaches : O(n)
two-simple-approaches-on-by-thisisakshat-7jyo
1.)Recursion: After doing level order traversal using recursion, we simply reverse the vectors at alternate levels\n\nclass Solution {\npublic:\n void helper
thisisakshat
NORMAL
2021-03-17T06:52:27.482277+00:00
2021-03-17T06:52:27.482313+00:00
7,761
false
**1.)Recursion:** After doing level order traversal using recursion, we simply reverse the vectors at alternate levels\n```\nclass Solution {\npublic:\n void helper(TreeNode*root,vector<vector<int>>&res,int level)\n {\n if(!root) return; \n if(level>=res.size())\n res.push_back({});\n ...
61
3
['Stack', 'Depth-First Search', 'Breadth-First Search', 'Recursion', 'Queue', 'C', 'C++']
8
binary-tree-zigzag-level-order-traversal
C++| Level Order Traversal | Beats 100% | BFS
c-level-order-traversal-beats-100-bfs-by-7lg3
Intuition\n Describe your first thoughts on how to solve this problem. \nSo Do Simple Level order but alternately revrse the elemnts of level.\n- Either we can
Xahoor72
NORMAL
2023-02-17T17:44:03.017809+00:00
2023-02-17T17:44:03.017992+00:00
10,120
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSo Do Simple Level order but alternately revrse the elemnts of level.\n- Either we can reverse the vector storing the elemnts f that level\n- Or we just insert from last index so that it will be in reverse order itself.(More Efficient)\n...
50
3
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'C++']
2
binary-tree-zigzag-level-order-traversal
🚀Simplest Solution🚀||🔥Full Explanation||🔥C++🔥|| Python3
simplest-solutionfull-explanationc-pytho-9e87
Consider\uD83D\uDC4D\n\n Please Upvote If You Find It Helpful\n\n# Intuition\nIntuition of this problem is very simple.\nWe use the Level Ord
naman_ag
NORMAL
2023-02-19T01:36:46.001514+00:00
2023-02-19T04:20:26.419287+00:00
7,726
false
# Consider\uD83D\uDC4D\n```\n Please Upvote If You Find It Helpful\n```\n# Intuition\nIntuition of this problem is very simple.\nWe use the **Level Order Traversal** of Binary tree.\nFor the Zig Zag Movement we are using a variable `level` when it is **zero** we **move from Left to Right** and when ...
41
2
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++', 'Python3']
8
binary-tree-zigzag-level-order-traversal
My Java Solution Beats 98%
my-java-solution-beats-98-by-biedengle2-6haa
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> res = new ArrayList();\n travel(res, 0, root);\n return
biedengle2
NORMAL
2016-03-16T19:34:41+00:00
2018-10-01T03:38:10.590066+00:00
13,761
false
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> res = new ArrayList();\n travel(res, 0, root);\n return res;\n }\n private void travel(List<List<Integer>> res, int level, TreeNode cur) {\n if (cur == null) return;\n if (res.size() <= level)...
37
1
['Breadth-First Search', 'Java']
6
binary-tree-zigzag-level-order-traversal
Java iterative and recursive solutions.
java-iterative-and-recursive-solutions-b-ltjd
\n // bfs \n public List<List<Integer>> zigzagLevelOrder1(TreeNode root) {\n List<List<Integer>> ret = new ArrayList<>();\n Queue<TreeNo
oldcodingfarmer
NORMAL
2016-05-07T07:56:46+00:00
2018-10-25T19:42:04.876984+00:00
9,754
false
\n // bfs \n public List<List<Integer>> zigzagLevelOrder1(TreeNode root) {\n List<List<Integer>> ret = new ArrayList<>();\n Queue<TreeNode> queue = new LinkedList<>();\n queue.add(root);\n int l = 0;\n while (!queue.isEmpty()) {\n int size = queue.size();\n ...
35
1
['Depth-First Search', 'Breadth-First Search', 'Java']
5
binary-tree-zigzag-level-order-traversal
JavaScript solution
javascript-solution-by-hongbo-miao-ymma
\nconst zigzagLevelOrder = (root) => {\n let res = [];\n\n const go = (node, lvl) => {\n if (node == null) return;\n if (res[lvl] == null) res[lvl] = []
hongbo-miao
NORMAL
2018-05-23T00:43:29.423454+00:00
2022-09-13T06:35:36.787337+00:00
4,412
false
```\nconst zigzagLevelOrder = (root) => {\n let res = [];\n\n const go = (node, lvl) => {\n if (node == null) return;\n if (res[lvl] == null) res[lvl] = [];\n\n if (lvl % 2 === 0) {\n res[lvl].push(node.val);\n } else {\n res[lvl].unshift(node.val);\n }\n\n go(node.left, lvl + 1);\n go(...
34
0
['JavaScript']
5
binary-tree-zigzag-level-order-traversal
Day 50 || BFS || Easiest Beginner Friendly Sol
day-50-bfs-easiest-beginner-friendly-sol-52zs
Intuition of this Problem:\nThe problem asks us to perform a level-order traversal of a binary tree, but with a slight modification - we need to return the node
singhabhinash
NORMAL
2023-02-19T01:48:48.184130+00:00
2023-02-19T02:07:58.427963+00:00
3,035
false
# Intuition of this Problem:\nThe problem asks us to perform a level-order traversal of a binary tree, but with a slight modification - we need to return the nodes of each level in a zigzag order, starting from left to right and then right to left for the next level, and so on.\n\nThe intuition behind this problem is t...
31
4
['Breadth-First Search', 'Binary Tree', 'C++', 'Java', 'Python3']
8
binary-tree-zigzag-level-order-traversal
Short and clear python code
short-and-clear-python-code-by-tusizi-9ndt
class Solution:\n # @param root, a tree node\n # @return a list of lists of integers\n def zigzagLevelOrder(self, root):\n queue = collections.d
tusizi
NORMAL
2015-02-18T16:57:24+00:00
2018-10-04T21:27:48.302743+00:00
7,741
false
class Solution:\n # @param root, a tree node\n # @return a list of lists of integers\n def zigzagLevelOrder(self, root):\n queue = collections.deque([root])\n res = []\n while queue:\n r = []\n for _ in range(len(queue)):\n q = queue.popleft()\n ...
30
0
['Python']
4
binary-tree-zigzag-level-order-traversal
✅ JAVA solution
java-solution-by-coding_menance-eipc
Code\nJAVA []\npublic class Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) \n {\n List<List<Integer>> sol = new ArrayList<
coding_menance
NORMAL
2023-02-19T06:05:59.948690+00:00
2023-02-19T06:05:59.948734+00:00
3,120
false
# Code\n``` JAVA []\npublic class Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) \n {\n List<List<Integer>> sol = new ArrayList<>();\n travel(root, sol, 0);\n return sol;\n }\n \n private void travel(TreeNode curr, List<List<Integer>> sol, int level)\n {\n...
28
1
['Java']
2
binary-tree-zigzag-level-order-traversal
Two-stacks JavaScript solution
two-stacks-javascript-solution-by-jeanti-p94c
We can easily solve this problem using two stacks. The first stack s1 is used to traverse the current level of the tree, and the second stack s2 is used to trac
jeantimex
NORMAL
2017-10-12T23:46:50.341000+00:00
2017-10-12T23:46:50.341000+00:00
1,926
false
We can easily solve this problem using two stacks. The first stack `s1` is used to traverse the current level of the tree, and the second stack `s2` is used to track the nodes in the next level. Also, we need to have a `flag` to indicate the traversal direction has been flipped when the current level has been traversed...
24
0
[]
3
binary-tree-zigzag-level-order-traversal
Javascript BFS
javascript-bfs-by-giddy-nw8v
Its a standard BFS with but the difference is we are keeping track of the depth at each level and that\'s determines if were adding to the front of the level a
giddy
NORMAL
2020-04-15T04:41:16.523999+00:00
2020-04-15T04:41:16.524040+00:00
2,458
false
Its a standard BFS with but the difference is we are keeping track of the depth at each level and that\'s determines if were adding to the front of the level array or the back of the level array. \n\n```\nvar zigzagLevelOrder = function(root) {\n if(!root) return [];\n let queue = [root];\n let output = [];\n let ...
21
0
['Breadth-First Search', 'JavaScript']
2
binary-tree-zigzag-level-order-traversal
Python iterative BFS using deque
python-iterative-bfs-using-deque-by-work-awmm
With even or odd level, change the pop/append direction of the deque accordingly.\n\n```\nfrom collections import deque\nclass Solution:\n def zigzagLevelOrd
workcool
NORMAL
2020-12-09T00:19:04.452944+00:00
2020-12-09T00:19:04.452982+00:00
1,547
false
With even or odd level, change the pop/append direction of the deque accordingly.\n\n```\nfrom collections import deque\nclass Solution:\n def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n if not root:\n return []\n \n dq = deque([root])\n res = []\n lvl =...
20
0
[]
0
binary-tree-zigzag-level-order-traversal
Clean C++ solution using two stacks [1 ms]
clean-c-solution-using-two-stacks-1-ms-b-2yj1
\nclass Solution {\npublic:\n \n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n \n stack<TreeNode*> LtoR, RtoL;\n int lvl =
devangarora
NORMAL
2020-08-17T13:12:29.975485+00:00
2020-08-17T16:24:46.252853+00:00
1,874
false
```\nclass Solution {\npublic:\n \n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n \n stack<TreeNode*> LtoR, RtoL;\n int lvl = 1;\n \n vector<vector<int>> ans;\n \n if(!root) return ans;\n\n RtoL.push(root);\n\n while(!RtoL.empty() || !LtoR....
20
1
['C']
1
binary-tree-zigzag-level-order-traversal
Simple and clear python solution with explain
simple-and-clear-python-solution-with-ex-ug7s
I use a additional function addLevel to record the level number of nodes, then according to the level number, I can easily deal with the level order, see the co
qinzhou
NORMAL
2015-03-22T12:48:51+00:00
2015-03-22T12:48:51+00:00
6,967
false
I use a additional function addLevel to record the level number of nodes, then according to the level number, I can easily deal with the level order, see the code for details\n\n # Definition for a binary tree node\n # class TreeNode:\n # def __init__(self, x):\n # self.val = x\n # s...
20
1
['Python']
4
binary-tree-zigzag-level-order-traversal
Video solution | Actual zig zag traversal | Complete intuition in detail | C++
video-solution-actual-zig-zag-traversal-ey1iq
Video\nHey everyone i have created a video solution for this problem where we will actually traverse the binary tree in zig zag order , in the video i will be t
_code_concepts_
NORMAL
2024-08-06T11:02:22.656808+00:00
2024-08-06T13:32:35.089948+00:00
1,992
false
# Video\nHey everyone i have created a video solution for this problem where we will actually traverse the binary tree in zig zag order , in the video i will be taking you through the complete thought process and intuition for the video, do watch till the end for complete explanantion.\n\nVideo link: https://youtu.be/1...
19
0
['C++']
0
binary-tree-zigzag-level-order-traversal
Simple java solution, 0ms,100 Faster, using queue, bfs
simple-java-solution-0ms100-faster-using-7ttk
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x)
kushguptacse
NORMAL
2020-04-01T12:15:11.985697+00:00
2020-04-01T12:15:11.985728+00:00
2,098
false
```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> op = new LinkedList...
19
0
['Breadth-First Search', 'Queue', 'Java']
0
binary-tree-zigzag-level-order-traversal
Java O(n) time O(log n) space | BFS | single Deque | 1ms
java-on-time-olog-n-space-bfs-single-deq-5mvu
The main idea is to \n1. Read from Front : left-to-right\n2. Write to Rear the left child followed by the right child\n3. Read from Rear : right-to-left\n4. Wri
black-panther
NORMAL
2020-07-22T10:28:51.946473+00:00
2020-07-22T10:28:51.946505+00:00
2,317
false
The main idea is to \n1. Read from Front : left-to-right\n2. Write to Rear the left child followed by the right child\n3. Read from Rear : right-to-left\n4. Write to Front the right child followed by the left child\n\nAs each item is removed from either end of the queue it is added to the result list\n![image](https://...
18
1
['Breadth-First Search', 'Queue', 'Java']
0
binary-tree-zigzag-level-order-traversal
c++ Simple code using 2 stacks O(n)
c-simple-code-using-2-stacks-on-by-vinee-lszq
\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) { \n vector<vector<int>> ans;\n if(!root) return ans;\n stack<TreeNode*> dqL
vineet20
NORMAL
2021-10-25T10:20:53.388054+00:00
2021-10-25T10:20:53.388093+00:00
782
false
```\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) { \n vector<vector<int>> ans;\n if(!root) return ans;\n stack<TreeNode*> dqL;\n stack<TreeNode*> dqR;\n TreeNode* cur = root;\n dqL.push(cur);\n \n while(!dqL.empty() || !dqR.empty()){\n ...
17
0
[]
3
binary-tree-zigzag-level-order-traversal
Easy Python solution || Level order traversal
easy-python-solution-level-order-travers-026l
\tdef zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n\t\t\tif not root:\n\t\t\t\treturn []\n\t\t\tlevel = 1\n\t\t\tres, curr, nxt = [], [root], []
ItsMeSheersendu
NORMAL
2021-07-18T20:20:32.609775+00:00
2021-07-18T20:20:32.609813+00:00
1,509
false
\tdef zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n\t\t\tif not root:\n\t\t\t\treturn []\n\t\t\tlevel = 1\n\t\t\tres, curr, nxt = [], [root], [] \n\t\t\twhile curr:\n\t\t\t\tfor i in curr:\n\t\t\t\t\tif i.left:\n\t\t\t\t\t\tnxt.append(i.left)\n\t\t\t\t\tif i.right:\n\t\t\t\t\t\tnxt.append(i.right)\n\t\t\...
15
1
['Python', 'Python3']
1
binary-tree-zigzag-level-order-traversal
📌Easy C++ Solution | Just a small change in Level Order
easy-c-solution-just-a-small-change-in-l-hyav
Obervesion: We just need to reverse the alternate levels, rest is same as Level Ordering.\n\n\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrd
em_ashutosh
NORMAL
2021-06-07T12:52:25.371851+00:00
2021-08-22T18:13:51.623391+00:00
1,858
false
Obervesion: We just need to reverse the alternate levels, rest is same as Level Ordering.\n\n```\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) \n {\n vector <vector<int>> v;\n if(!root) // if root is null return\n return v;\n queue <TreeNode*> q;...
15
1
['C', 'C++']
1
binary-tree-zigzag-level-order-traversal
C++ BFS
c-bfs-by-rohitsaraf17-3sal
\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(root == NULL)\n
rohitsaraf17
NORMAL
2020-01-05T23:06:39.882635+00:00
2020-01-05T23:06:39.882674+00:00
2,051
false
```\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(root == NULL)\n return ans;\n queue<TreeNode*> info;\n info.push(root);\n bool flag = false;\n while(!info.empty()){\n int n = info.s...
15
0
['Breadth-First Search', 'C']
6
binary-tree-zigzag-level-order-traversal
Python easy to understand deque solution
python-easy-to-understand-deque-solution-yvmx
\nclass Solution(object):\n def zigzagLevelOrder(self, root):\n deque = collections.deque()\n if root:\n deque.append(root)\n
oldcodingfarmer
NORMAL
2015-07-14T19:43:28+00:00
2020-10-15T14:26:21.118133+00:00
3,808
false
```\nclass Solution(object):\n def zigzagLevelOrder(self, root):\n deque = collections.deque()\n if root:\n deque.append(root)\n res, level = [], 0\n while deque:\n l, size = [], len(deque)\n for _ in range(size): # process level by level\n ...
15
0
['Breadth-First Search', 'Queue', 'Python']
3
binary-tree-zigzag-level-order-traversal
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
leetcode-the-hard-way-explained-line-by-o53hh
\uD83D\uDD34 Check out LeetCode The Hard Way for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our Discord Study Group for live discussion.
__wkw__
NORMAL
2023-02-19T03:04:14.912837+00:00
2023-02-19T03:04:14.912887+00:00
2,361
false
\uD83D\uDD34 Check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our [Discord Study Group](https://discord.gg/Nqm4jJcyBf) for live discussion.\n\uD83D\uDFE2 Give a star on [Github Repository](https://github.com/wing...
14
2
['Breadth-First Search', 'Python']
3
binary-tree-zigzag-level-order-traversal
My AC Java code
my-ac-java-code-by-lurklurk-0lwl
I use two stacks, one for processing current layer and one for storing nodes for the next layer. I also use a flag (order in your code) to indicate the directio
lurklurk
NORMAL
2014-09-13T20:22:57+00:00
2014-09-13T20:22:57+00:00
5,057
false
I use two stacks, one for processing current layer and one for storing nodes for the next layer. I also use a flag (order in your code) to indicate the direction. It is straightforward\n\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> output = new ArrayList<List<Integer>>(...
14
0
[]
2
binary-tree-zigzag-level-order-traversal
[recommend for beginners]clean C++ implementation with detailed explanation
recommend-for-beginnersclean-c-implement-1h4n
class Solution {\n public:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> result;\n if(!root) re
rainbowsecret
NORMAL
2016-01-27T13:27:03+00:00
2018-10-26T02:13:25.211267+00:00
3,585
false
class Solution {\n public:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> result;\n if(!root) return result;\n deque<TreeNode*> tree;\n tree.push_back(root);\n int flag=0;\n while(!tree.empty()){\n ...
14
11
[]
6
binary-tree-zigzag-level-order-traversal
✔️ 100% Fastest Swift Solution, time: O(n), space: O(n).
100-fastest-swift-solution-time-on-space-4qjo
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right
sergeyleschev
NORMAL
2022-04-09T05:44:54.430598+00:00
2022-04-09T05:44:54.430645+00:00
486
false
```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right: TreeNode?\n * public init() { self.val = 0; self.left = nil; self.right = nil; }\n * public init(_ val: Int) { self.val = val; self.left = nil; sel...
13
0
['Swift']
1
binary-tree-zigzag-level-order-traversal
0ms beats 100%. Easy to understand recursive solution.
0ms-beats-100-easy-to-understand-recursi-bq6s
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x)
gullible_frog
NORMAL
2019-11-25T03:39:55.012508+00:00
2019-11-25T03:41:09.525837+00:00
1,356
false
```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n \n List<List<Integer>> res = new...
13
0
['Recursion', 'Java']
3
binary-tree-zigzag-level-order-traversal
C++ ✅ || Beats 100%💯 || Fast Solution🔥|| Simple Code🚀
c-beats-100-fast-solution-simple-code-by-z867
🌟 Intuition"Let's go zigzag through the tree, level by level!" This problem is like navigating a forest, but with a twist! Instead of moving straight, we need t
yashm01
NORMAL
2024-12-27T08:11:46.812160+00:00
2024-12-27T08:11:46.812160+00:00
1,723
false
![Screenshot 2024-12-27 133942.png](https://assets.leetcode.com/users/images/6730777b-bde8-4e03-a62a-88363e92d832_1735287033.327401.png) # 🌟 Intuition *"Let's go zigzag through the tree, level by level!"* This problem is like navigating a forest, but with a twist! Instead of moving straight, we need to alternate ...
12
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']
0