Skip to content

Commit 0134d8c

Browse files
add post '(Leetcode) 124 - Binary Tree Maximum Path Sum'
1 parent 68c7856 commit 0134d8c

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

_posts/2024-02-15-leetcode-141.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tags:
1818
fast,
1919
]
2020
date: 2024-02-15 22:00:00 +0900
21+
toc: true
2122
---
2223

2324
[New Year Gift - Curated List of Top 75 LeetCode Questions to Save Your Time](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)

_posts/2024-03-31-leetcode-226.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: (Leetcode) 226 - invert binary tree
44
categories: [스터디-알고리즘]
55
tags: [자바, java, 트리, Tree, 리트코드, Leetcode, 알고리즘, algorithm]
66
date: 2024-03-31 15:30:00 +0900
7+
toc: true
78
---
89

910
[New Year Gift - Curated List of Top 75 LeetCode Questions to Save Your Time](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)
@@ -16,6 +17,8 @@ date: 2024-03-31 15:30:00 +0900
1617

1718
tree 문제다.
1819

20+
## 내가 작성한 풀이
21+
1922
자바로 한번 풀어봤다.
2023

2124
eazy 중에 eazy 한 문제라 그냥 풀었더니 바로 통과했다.

_posts/2024-05-03-leetcode-124.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
layout: post
3+
title: (Leetcode) 124 - Binary Tree Maximum Path Sum
4+
categories: [스터디-알고리즘]
5+
tags:
6+
[자바, java, graph, 그래프, 리트코드, Leetcode, 알고리즘, algorithm, neetcode]
7+
date: 2024-05-01 00:30:00 +0900
8+
toc: true
9+
---
10+
11+
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.
12+
13+
[https://neetcode.io/practice](https://neetcode.io/practice)
14+
15+
---
16+
17+
주간 미션을 마쳤기 때문에 시간이 남아서 뒤에서 한문제를 골라 풀기로 하였다.
18+
19+
[https://leetcode.com/problems/binary-tree-maximum-path-sum/description/](https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
20+
21+
이 문제는 hard 문제이다. 굉장히 어려웠다. 아무리 생각해도 효율적인 방법이 생각나지 않아서 솔루션을 확인하였다. 다음에 꼭 다시 풀어봐야겠다.
22+
23+
## 모범 답안
24+
25+
문제에서 어려웠던 부분은 'ㅅ' 모양으로 도 path가 설정될 수 있다는 것이였다.
26+
27+
문제에서 제공된 binary tree는 위에서 아래로 내려갈수는 있지만 다시 올라갈수는 없는 구조였다.
28+
그래서 root에서 시작해야 한다는 것 정도는 힌트로 얻을 수 있었었다.
29+
30+
```java
31+
class Solution {
32+
int max = Integer.MIN_VALUE;
33+
34+
public int maxPath(TreeNode root) {
35+
if(root == null) return 0;
36+
37+
int value = root.val;
38+
39+
int left_sum = Math.max(maxPath(root.left),0);
40+
int right_sum = Math.max(maxPath(root.right),0);
41+
42+
max = Math.max(max, left_sum + right_sum + value);
43+
44+
return Math.max(left_sum, right_sum) + value;
45+
}
46+
47+
public int maxPathSum(TreeNode root) {
48+
maxPath(root);
49+
return max;
50+
}
51+
}
52+
```
53+
54+
ㅅ 자 모양으로 계산했을 때의 값은 max에 보관한다. 부모 노드에는 왼쪽과 오른쪽 중에서 큰 값과 자신의 값을 더한 값을 전달해준다. 이를 통해서 path가 중복되지 않으면서 계산을 할 수 있다.
55+
56+
### TC, SC
57+
58+
시간 복잡도는 O(n)이고, 공간 복잡도는 O(h)이다. h는 트리의 높이이다. 최악의 경우 n이 될수도 있다. (linked list 처럼 한 줄로 연결되는 경우)

0 commit comments

Comments
 (0)