Skip to content

Commit f201a57

Browse files
authored
clarify space complexity for dfs trees (neetcode-gh#3761)
1 parent 232acfa commit f201a57

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

articles/balanced-binary-tree.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,11 @@ class Solution {
526526
### Time & Space Complexity
527527

528528
* Time complexity: $O(n)$
529-
* Space complexity: $O(n)$
529+
* Space complexity: $O(h)$
530+
* Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$
531+
* Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$
532+
533+
> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree.
530534
531535
---
532536

articles/binary-tree-diameter.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,11 @@ class Solution {
520520
### Time & Space Complexity
521521

522522
* Time complexity: $O(n)$
523-
* Space complexity: $O(n)$
523+
* Space complexity: $O(h)$
524+
* Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$
525+
* Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$
526+
527+
> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree.
524528

525529
---
526530

articles/depth-of-binary-tree.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ class Solution {
176176
### Time & Space Complexity
177177

178178
* Time complexity: $O(n)$
179-
* Space complexity: $O(n)$
179+
* Space complexity: $O(h)$
180+
* Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$
181+
* Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$
182+
183+
> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree.
180184
181185
---
182186

articles/same-binary-tree.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ class Solution {
191191
### Time & Space Complexity
192192

193193
* Time complexity: $O(n)$
194-
* Space complexity: $O(n)$
194+
* Space complexity: $O(h)$
195+
* Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$
196+
* Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$
197+
198+
> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree.
195199

196200
---
197201

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy