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

Commit 07a19ac

Browse files
Merge pull request #342 from MoigeMatino/add-how-high-v7
feat: add how high v7
2 parents a256d29 + 06a958e commit 07a19ac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎binary_tree/how_high/how_high_v7.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def how_high(root):
2+
if root is None:
3+
return -1
4+
5+
stack = [(root,0)]
6+
max_height = float("-inf")
7+
8+
while stack:
9+
current, current_height = stack.pop()
10+
11+
if current.left is None and current.right is None:
12+
if current_height > max_height:
13+
max_height = current_height
14+
15+
if current.right:
16+
stack.append((current.right, current_height+1))
17+
18+
if current.left:
19+
stack.append((current.left, current_height+1))
20+
21+
return max_height

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /