일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- DWG
- 밴픽
- 운영체제
- Protocol
- shiba
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- 30. Substring with Concatenation of All Words
- concurrency
- kaggle
- Python
- Python Implementation
- 시바견
- Decorator
- LeetCode
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- iterator
- Substring with Concatenation of All Words
- Regular Expression
- Generator
- Class
- 프로그래머스
- data science
- 43. Multiply Strings
- 파이썬
- t1
- Python Code
- attribute
- 715. Range Module
Archives
- Today
- Total
Scribbling
LeetCode: 337. House Robber III 본문
오늘 도둑은 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 rob(self, root: Optional[TreeNode]) -> int:
a, b = self.dfs(root)
return max(a, b)
def dfs(self, node):
if not node.left and not node.right:
return 0, node.val
if node.left and node.right:
a, b = self.dfs(node.left)
c, d = self.dfs(node.right)
return max(a+c, a+d, b+c, b+d), node.val+a+c
if node.left:
a, b = self.dfs(node.left)
return max(a,b), a+node.val
a, b = self.dfs(node.right)
return max(a,b), a+node.val
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 968. Binary Tree Cameras (0) | 2021.12.13 |
---|---|
LeetCode: 4. Median of Two Sorted Arrays (0) | 2021.12.12 |
LeetCode: 215. Kth Largest Element in an Array (0) | 2021.12.11 |
LeetCode: 208. Implement Trie (Prefix Tree) (0) | 2021.12.09 |
LeetCode: 136. Single Number (0) | 2021.12.09 |