일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Python Implementation
- 시바견
- data science
- Regular Expression
- LeetCode
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- Protocol
- Python
- DWG
- kaggle
- concurrency
- shiba
- 30. Substring with Concatenation of All Words
- Generator
- Decorator
- attribute
- 운영체제
- iterator
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- Class
- 43. Multiply Strings
- 파이썬
- t1
- Python Code
- 프로그래머스
- 컴퓨터의 구조
- 715. Range Module
- 밴픽
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 |