-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_tree.py
More file actions
52 lines (38 loc) · 1.01 KB
/
Copy pathexample_tree.py
File metadata and controls
52 lines (38 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Tree Visualization Example
SETUP:
1. Set breakpoint on line 42 (the return statement)
2. Press F5 to debug
3. Press Shift+F1 to open visualizer
4. Configure:
- Expression: tree
- Pointer 1: root | Type: Object | Color: green
5. Check "Auto-refresh on step"
6. Press F11 (Step Into) to go into recursive calls
IMPORTANT: Select "Object" type (not Index) for tree node tracking!
"""
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# Build tree:
# 1
# / \
# 2 3
# / \
# 4 5
tree = TreeNode(1,
TreeNode(2, TreeNode(4), TreeNode(5)),
TreeNode(3)
)
def maxDepth(root: Optional[TreeNode]) -> int:
if not root:
return 0
left = maxDepth(root.left) # F11 to step into
right = maxDepth(root.right)
return max(left, right) + 1 # <-- BREAKPOINT HERE
print("Starting maxDepth...")
result = maxDepth(tree)
print(f"Max depth: {result}")