From f201a57b8286a9bdd4212d12950fdd7d3f14b3f3 Mon Sep 17 00:00:00 2001 From: rey Date: Sat, 7 Dec 2024 09:55:57 -0800 Subject: [PATCH] clarify space complexity for dfs trees (#3761) --- articles/balanced-binary-tree.md | 6 +++++- articles/binary-tree-diameter.md | 6 +++++- articles/depth-of-binary-tree.md | 6 +++++- articles/same-binary-tree.md | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/articles/balanced-binary-tree.md b/articles/balanced-binary-tree.md index 65a8aece1..c9c407ed0 100644 --- a/articles/balanced-binary-tree.md +++ b/articles/balanced-binary-tree.md @@ -526,7 +526,11 @@ class Solution { ### Time & Space Complexity * Time complexity: $O(n)$ -* Space complexity: $O(n)$ +* Space complexity: $O(h)$ + * Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$ + * Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$ + +> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree. --- diff --git a/articles/binary-tree-diameter.md b/articles/binary-tree-diameter.md index d6c7ab0fe..3c73fbe92 100644 --- a/articles/binary-tree-diameter.md +++ b/articles/binary-tree-diameter.md @@ -520,7 +520,11 @@ class Solution { ### Time & Space Complexity * Time complexity: $O(n)$ -* Space complexity: $O(n)$ +* Space complexity: $O(h)$ + * Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$ + * Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$ + +> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree. --- diff --git a/articles/depth-of-binary-tree.md b/articles/depth-of-binary-tree.md index d14965dba..efd76b64d 100644 --- a/articles/depth-of-binary-tree.md +++ b/articles/depth-of-binary-tree.md @@ -176,7 +176,11 @@ class Solution { ### Time & Space Complexity * Time complexity: $O(n)$ -* Space complexity: $O(n)$ +* Space complexity: $O(h)$ + * Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$ + * Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$ + +> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree. --- diff --git a/articles/same-binary-tree.md b/articles/same-binary-tree.md index 05876d926..6cbfc12aa 100644 --- a/articles/same-binary-tree.md +++ b/articles/same-binary-tree.md @@ -191,7 +191,11 @@ class Solution { ### Time & Space Complexity * Time complexity: $O(n)$ -* Space complexity: $O(n)$ +* Space complexity: $O(h)$ + * Best Case ([balanced tree](https://www.geeksforgeeks.org/balanced-binary-tree/)): $O(log(n))$ + * Worst Case ([degenerate tree](https://www.geeksforgeeks.org/introduction-to-degenerate-binary-tree/)): $O(n)$ + +> Where $n$ is the number of nodes in the tree and $h$ is the height of the tree. ---