>>> root = Node(1) >>> root.depth() 1 >>> root.left = Node(2) >>> root.depth() 2 >>> root.left.depth() 1 >>> root.right = Node(3) >>> root.depth() 2
(self)
| 15 | right: Node | None = None |
| 16 | |
| 17 | def depth(self) -> int: |
| 18 | """ |
| 19 | >>> root = Node(1) |
| 20 | >>> root.depth() |
| 21 | 1 |
| 22 | >>> root.left = Node(2) |
| 23 | >>> root.depth() |
| 24 | 2 |
| 25 | >>> root.left.depth() |
| 26 | 1 |
| 27 | >>> root.right = Node(3) |
| 28 | >>> root.depth() |
| 29 | 2 |
| 30 | """ |
| 31 | left_depth = self.left.depth() if self.left else 0 |
| 32 | right_depth = self.right.depth() if self.right else 0 |
| 33 | return max(left_depth, right_depth) + 1 |
| 34 | |
| 35 | def diameter(self) -> int: |
| 36 | """ |