Skip to content

Commit c67cec4

Browse files
committed
update readme
1 parent 99da292 commit c67cec4

File tree

6 files changed

+65
-143
lines changed

6 files changed

+65
-143
lines changed

0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0104-maximum-depth-of-binary-tree/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,67 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode:
3-
# def __init__(self, val=0, left=None, right=None):
4-
# self.val = val
5-
# self.left = left
6-
# self.right = right
1+
# time complexity: O(n)
2+
# space complexity: O(logn)
3+
from typing import Optional
4+
5+
6+
class TreeNode:
7+
def __init__(self, val=0, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
713
class Solution:
814
def maxDepth(self, root: Optional[TreeNode]) -> int:
9-
1015
def longestPath(node: Optional[TreeNode]):
1116
if not node:
1217
return 0
1318
leftPath = longestPath(node.left)
1419
rightPath = longestPath(node.right)
1520
return max(leftPath, rightPath)+1
21+
return longestPath(root)
22+
23+
24+
# time complexity: O(n)
25+
# space complexity: O(n)
26+
class Solution:
27+
def __init__(self):
28+
self.nextItem = []
29+
self.maxDepth = 0
30+
31+
def nextMaxDepth(self):
32+
if not self.nextItem:
33+
return self.maxDepth
34+
nextNode, nextLvl = self.nextItem.pop(0)
35+
nextLvl += 1
36+
self.maxDepth = max(self.maxDepth, nextLvl)
37+
if nextNode.left:
38+
self.nextItem.append((nextNode.left, nextLvl))
39+
if nextNode.right:
40+
self.nextItem.append((nextNode.right, nextLvl))
41+
return self.nextMaxDepth()
42+
43+
def maxDepth(self, root):
44+
if not root:
45+
return 0
46+
self.nextItem = []
47+
self.maxDepth = 0
48+
self.nextItem.append((root, 0))
49+
return self.nextMaxDepth()
50+
51+
# time complexity: O(n)
52+
# space complexity: O(n)
53+
class Solution:
54+
def maxDepth(self, root: TreeNode) -> int:
55+
stack = []
56+
if root is not None:
57+
stack.append((1, root))
58+
59+
depth = 0
60+
while stack != []:
61+
currDepth, root = stack.pop()
62+
if root is not None:
63+
depth = max(depth, currDepth)
64+
stack.append((currDepth + 1, root.left))
65+
stack.append((currDepth + 1, root.right))
1666

17-
return longestPath(root)
67+
return depth
File renamed without changes.

README.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,39 +115,3 @@ It helps others discover the repo and keeps the project growing.
115115
---
116116

117117
Feedback / Questions → open an Issue or reach out on [LinkedIn](https://www.linkedin.com/in/hogan-l/)
118-
119-
<!---LeetCode Topics Start-->
120-
# LeetCode Topics
121-
## Tree
122-
| |
123-
| ------- |
124-
| [0104-maximum-depth-of-binary-tree](https://github.com/hogan-tech/leetcode-solution/tree/master/0104-maximum-depth-of-binary-tree) |
125-
## Depth-First Search
126-
| |
127-
| ------- |
128-
| [0104-maximum-depth-of-binary-tree](https://github.com/hogan-tech/leetcode-solution/tree/master/0104-maximum-depth-of-binary-tree) |
129-
## Breadth-First Search
130-
| |
131-
| ------- |
132-
| [0104-maximum-depth-of-binary-tree](https://github.com/hogan-tech/leetcode-solution/tree/master/0104-maximum-depth-of-binary-tree) |
133-
## Binary Tree
134-
| |
135-
| ------- |
136-
| [0104-maximum-depth-of-binary-tree](https://github.com/hogan-tech/leetcode-solution/tree/master/0104-maximum-depth-of-binary-tree) |
137-
## Hash Table
138-
| |
139-
| ------- |
140-
| [0768-partition-labels](https://github.com/hogan-tech/leetcode-solution/tree/master/0768-partition-labels) |
141-
## Two Pointers
142-
| |
143-
| ------- |
144-
| [0768-partition-labels](https://github.com/hogan-tech/leetcode-solution/tree/master/0768-partition-labels) |
145-
## String
146-
| |
147-
| ------- |
148-
| [0768-partition-labels](https://github.com/hogan-tech/leetcode-solution/tree/master/0768-partition-labels) |
149-
## Greedy
150-
| |
151-
| ------- |
152-
| [0768-partition-labels](https://github.com/hogan-tech/leetcode-solution/tree/master/0768-partition-labels) |
153-
<!---LeetCode Topics End-->

Readme/0104-maximum-depth-of-binary-tree.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
<h2><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">104. Maximum Depth of Binary Tree</a></h2><h3>Easy</h3><hr><div><p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p>
1+
<h2><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree">104. Maximum Depth of Binary Tree</a></h2><h3>Easy</h3><hr><p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p>
22

3-
<p>A binary tree's <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
3+
<p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
44

55
<p>&nbsp;</p>
66
<p><strong class="example">Example 1:</strong></p>
7-
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;">
8-
<pre><strong>Input:</strong> root = [3,9,20,null,null,15,7]
7+
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" />
8+
<pre>
9+
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
910
<strong>Output:</strong> 3
1011
</pre>
1112

1213
<p><strong class="example">Example 2:</strong></p>
1314

14-
<pre><strong>Input:</strong> root = [1,null,2]
15+
<pre>
16+
<strong>Input:</strong> root = [1,null,2]
1517
<strong>Output:</strong> 2
1618
</pre>
1719

@@ -22,4 +24,3 @@
2224
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
2325
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
2426
</ul>
25-
</div>

0 commit comments

Comments
 (0)