Skip to content

Commit d4b10e9

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents bb916e1 + 1d76cbb commit d4b10e9

File tree

515 files changed

+20421
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

515 files changed

+20421
-205
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* [Problem]: [124 Binary Tree Maximum Path Sum
3+
* (https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
4+
*/
5+
/**
6+
* Definition for a binary tree node.
7+
* class TreeNode {
8+
* val: number
9+
* left: TreeNode | null
10+
* right: TreeNode | null
11+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
12+
* this.val = (val===undefined ? 0 : val)
13+
* this.left = (left===undefined ? null : left)
14+
* this.right = (right===undefined ? null : right)
15+
* }
16+
* }
17+
*/
18+
19+
//시간복잡도 O(n)
20+
//공간복잡도 O(h)
21+
function maxPathSum(root: TreeNode | null): number {
22+
if (!root) return 0;
23+
let result = root.val;
24+
25+
function dfs(node: TreeNode | null) {
26+
if (!node) return 0;
27+
28+
let leftMax = Math.max(dfs(node.left), 0);
29+
let rightMax = Math.max(dfs(node.right), 0);
30+
31+
result = Math.max(node.val + leftMax + rightMax, result);
32+
33+
return node.val + Math.max(leftMax, rightMax);
34+
}
35+
36+
dfs(root);
37+
return result;
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
// TC: O(n)
13+
// SC: O(n)
14+
function maxPathSum(root: TreeNode | null): number {
15+
let maxSum = -Infinity;
16+
17+
const dfs = (node: TreeNode | null) => {
18+
if (!node) return 0;
19+
20+
const leftMax = Math.max(dfs(node.left), 0);
21+
const rightMax = Math.max(dfs(node.right), 0);
22+
23+
maxSum = Math.max(node.val + leftMax + rightMax, maxSum);
24+
25+
return node.val + Math.max(leftMax, rightMax);
26+
};
27+
28+
dfs(root);
29+
return maxSum;
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int dfs(TreeNode* root, int& result){
4+
if(root == nullptr)
5+
return 0;
6+
7+
int left = max(0, dfs(root->left, result));
8+
int right = max(0, dfs(root->right, result));
9+
int sum = root->val + left + right;
10+
11+
result = max(result, sum);
12+
13+
return root->val + max(left, right);
14+
}
15+
16+
int maxPathSum(TreeNode* root) {
17+
int result = INT_MIN;
18+
19+
dfs(root, result);
20+
return result;
21+
}
22+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
// 최대 Path 누적 합
19+
int maxPathSumVal = Integer.MIN_VALUE;
20+
21+
// 시간복잡도: O(n), 공간복잡도: O(h) h as height of tree
22+
public int maxPathSum(TreeNode root) {
23+
maxPathSumChecker(root);
24+
return maxPathSumVal;
25+
}
26+
27+
// 재귀
28+
private int maxPathSumChecker(TreeNode node) {
29+
30+
if (node == null) {
31+
return 0;
32+
}
33+
34+
int leftMax = Math.max(maxPathSumChecker(node.left), 0);
35+
int rightMax = Math.max(maxPathSumChecker(node.right), 0);
36+
37+
maxPathSumVal = Math.max(node.val + leftMax + rightMax, maxPathSumVal);
38+
39+
return node.val + Math.max(leftMax, rightMax);
40+
}
41+
}
42+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Optional
2+
3+
class TreeNode:
4+
def __init__(self, val=0, left=None, right=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
class Solution:
9+
"""
10+
- Time Complexity: O(n), n = The number of nodes
11+
- Space Complexity: O(H), H = The height of Tree
12+
"""
13+
def maxPathSum(self, root: Optional[TreeNode]) -> int:
14+
self.max_sum = float("-inf")
15+
16+
def dfs(node):
17+
if not node:
18+
return 0
19+
20+
# Find max sum from the left and right children (more than 0)
21+
l_sum = max(dfs(node.left), 0)
22+
r_sum = max(dfs(node.right), 0)
23+
24+
# Calculate current sum and update max_sum
25+
current_sum = node.val + l_sum + r_sum
26+
self.max_sum = max(self.max_sum, current_sum)
27+
28+
# Return larger path
29+
return node.val + max(l_sum, r_sum)
30+
31+
dfs(root)
32+
33+
return self.max_sum
34+
35+
def do_test():
36+
sol = Solution()
37+
38+
root1 = TreeNode(1)
39+
root1.left = TreeNode(2)
40+
root1.right = TreeNode(3)
41+
e1 = 6
42+
r1 = sol.maxPathSum(root1)
43+
print(f"TC 1 is Passed!" if r1 == e1 else f"TC 1 is Failed! - Expected: {e1}, Result: {r1}")
44+
45+
root2 = TreeNode(-10)
46+
root2.left = TreeNode(9)
47+
root2.right = TreeNode(20)
48+
root2.right.left = TreeNode(15)
49+
root2.right.right = TreeNode(7)
50+
e2 = 42
51+
r2 = sol.maxPathSum(root2)
52+
print(f"TC 2 is Passed!" if r2 == e2 else f"TC 2 is Failed! - Expected: {e2}, Result: {r2}")
53+
54+
do_test()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/binary-tree-maximum-path-sum/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
var maxPathSum = function (root) {
15+
let maxSum = -Infinity; // global max
16+
17+
function dfs(node) {
18+
if (!node) return 0;
19+
20+
// 왼쪽과 오른쪽 서브트리에서 최대 경로 합을 구한다
21+
// 음수면 0으로 치환 (해당 서브트리를 포함하지 않는게 더 이득인 경우)
22+
let leftMax = Math.max(0, dfs(node.left));
23+
let rightMax = Math.max(0, dfs(node.right));
24+
25+
// 현재 노드를 루트로 하는 경로에서 최대값을 계산 (left + node + right)
26+
let currentMax = leftMax + node.val + rightMax;
27+
28+
// global 최대값 갱신
29+
maxSum = Math.max(maxSum, currentMax);
30+
31+
// 부모 노드로 return 시: 한쪽 방향으로만 선택 가능
32+
return node.val + Math.max(leftMax, rightMax);
33+
}
34+
35+
dfs(root);
36+
return maxSum;
37+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maxPathSum(TreeNode* root) {
4+
int res = INT_MIN;
5+
6+
dfs(root, res);
7+
return res;
8+
}
9+
10+
int dfs(TreeNode* root, int& res) {
11+
if (!root)
12+
return 0;
13+
14+
// 좌측 부분트리, 우측 부분트리를 각각 계산할 때 dfs의 '반환값'을 활용
15+
// 음수값이 나올 경우는 0으로 대체함
16+
int left = max(0, dfs(root->left, res));
17+
int right = max(0, dfs(root->right, res));
18+
19+
// 최종 반환할 값 업데이트
20+
// 좌측 트리, 우측 트리, 루트 노드를 모두 통과하는 경로가 현재까지 최댓값인지 비교해서 갱신
21+
res = max(res, root->val + left + right);
22+
23+
// 상위 노드로 전달되는 값은 root의 값을 포함하는 경로의 값이다
24+
// 따라서 좌측 트리 혹은 우측 트리 중 하나의 경로만을 선택해서 통과할 수 있다
25+
return root->val + max(left, right);
26+
}
27+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val);
5+
* this.left = (left===undefined ? null : left);
6+
* this.right = (right===undefined ? null : right);
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
var maxPathSum = function(root) {
15+
let res = [root.val];
16+
17+
function dfs(node) {
18+
if (!node) return 0;
19+
20+
let leftMax = dfs(node.left);
21+
let rightMax = dfs(node.right);
22+
23+
leftMax = Math.max(leftMax, 0);
24+
rightMax = Math.max(rightMax, 0);
25+
26+
// 경유지점 포함한 경로 최대값 업데이트
27+
res[0] = Math.max(res[0], node.val + leftMax + rightMax);
28+
29+
// 분기하지 않는 경로에서의 최대값 리턴
30+
return node.val + Math.max(leftMax, rightMax);
31+
}
32+
33+
dfs(root);
34+
return res[0];
35+
};
36+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/binary-tree-maximum-path-sum/">week11-5. binary-tree-maximum-path-sum</a>
3+
* <li>Description: Given the root of a binary tree, return the maximum path sum of any non-empty path</li>
4+
* <li>Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(H), Memory 44.1MB</li>
7+
*/
8+
class Solution {
9+
public int maxPathSum(TreeNode root) {
10+
dfs(root);
11+
return max;
12+
}
13+
14+
int max = Integer.MIN_VALUE;
15+
public int dfs(TreeNode head) {
16+
if (head == null) return 0;
17+
18+
int left = Math.max(0, dfs(head.left));
19+
int right = Math.max(0, dfs(head.right));
20+
21+
max = Math.max(max, head.val + left + right);
22+
23+
return head.val + Math.max(left, right);
24+
}
25+
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var maxPathSum = function (root) {
2+
let maxSum = -Infinity;
3+
4+
function dfs(node) {
5+
if (node === null) return 0; // 6) Base Case
6+
const left = Math.max(dfs(node.left), 0); // 8) Pruning
7+
const right = Math.max(dfs(node.right), 0); // 8) Pruning
8+
9+
const currentSum = left + node.val + right; // 9) Pivot Sum
10+
maxSum = Math.max(maxSum, currentSum); // 7) Global Max
11+
12+
return node.val + Math.max(left, right); // 10) Return Value
13+
}
14+
15+
dfs(root); // 4) DFS 시작
16+
console.log(maxSum); // 최종 답 출력
17+
};

0 commit comments

Comments
 (0)