일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 컴퓨터의 구조
- 밴픽
- 프로그래머스
- data science
- 43. Multiply Strings
- 30. Substring with Concatenation of All Words
- Generator
- Protocol
- Substring with Concatenation of All Words
- shiba
- Regular Expression
- iterator
- DWG
- Python
- attribute
- concurrency
- LeetCode
- Class
- 파이썬
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- Python Implementation
- Python Code
- Decorator
- 시바견
- t1
- 운영체제
- 715. Range Module
- kaggle
- Convert Sorted List to Binary Search Tree
Archives
- Today
- Total
Scribbling
LeetCode: 366. Find Leaves of Binary Tree 본문
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findLeaves(self, root: Optional[TreeNode]) -> List[List[int]]:
self.ret = []
self.dfs(root)
return self.ret
def dfs(self, node):
if node.left and node.right:
h = max(self.dfs(node.left), self.dfs(node.right)) + 1
elif node.left:
h = self.dfs(node.left) + 1
elif node.right:
h = self.dfs(node.right) + 1
else:
h = 0
if h == len(self.ret):
self.ret.append([])
self.ret[h].append(node.val)
return h
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 315. Count of Smaller Numbers After Self (0) | 2022.02.10 |
---|---|
LeetCode: 135. Candy (0) | 2022.02.03 |
LeetCode: 109. Convert Sorted List to Binary Search Tree (0) | 2022.01.26 |
LeetCode: 302. Smallest Rectangle Enclosing Black Pixels (0) | 2022.01.25 |
LeetCode: 65. Valid Number (0) | 2022.01.20 |