Skip to content

Commit 0b864cc

Browse files
committed
leetcode
1 parent 8ba0e91 commit 0b864cc

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
4+
-* 124. Binary Tree Maximum Path Sum *-
5+
6+
7+
8+
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
9+
10+
The path sum of a path is the sum of the node's values in the path.
11+
12+
Given the root of a binary tree, return the maximum path sum of any non-empty path.
13+
14+
15+
16+
Example 1:
17+
18+
19+
Input: root = [1,2,3]
20+
Output: 6
21+
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
22+
Example 2:
23+
24+
25+
Input: root = [-10,9,20,null,null,15,7]
26+
Output: 42
27+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
28+
29+
30+
Constraints:
31+
32+
The number of nodes in the tree is in the range [1, 3 * 104].
33+
-1000 <= Node.val <= 1000
34+
35+
*/
36+
37+
// Definition for a binary tree node.
38+
import 'dart:math';
39+
40+
class TreeNode {
41+
int val;
42+
TreeNode? left;
43+
TreeNode? right;
44+
TreeNode([this.val = 0, this.left, this.right]);
45+
}
46+
47+
class A {
48+
int val = -1000;
49+
int maxPathSum(TreeNode? root) {
50+
postOrder(root);
51+
return val;
52+
}
53+
54+
int postOrder(TreeNode? root) {
55+
if (root == null) return 0;
56+
int sumLeft = postOrder(root.left);
57+
int sumRight = postOrder(root.right);
58+
int maxi = max(
59+
root.val,
60+
max(root.val + sumLeft,
61+
max(root.val + sumRight, root.val + sumRight + sumLeft)));
62+
if (maxi > val) val = maxi;
63+
return root.val > root.val + max(sumLeft, sumRight)
64+
? root.val
65+
: root.val + max(sumLeft, sumRight);
66+
}
67+
}
68+
69+
class B {
70+
int maxPathSum(TreeNode? root) {
71+
List<int> maxi = [1];
72+
maxi[0] = -1000;
73+
pathDown(maxi, root);
74+
return maxi[0];
75+
}
76+
77+
int pathDown(List<int> maxi, TreeNode? root) {
78+
if (root == null) return 0;
79+
int leftMax = max(0, pathDown(maxi, root.left));
80+
int rightMax = max(0, pathDown(maxi, root.right));
81+
maxi[0] = max(maxi[0], root.val + leftMax + rightMax);
82+
return root.val + max(leftMax, rightMax);
83+
}
84+
}
85+
86+
class C {
87+
int maxPathSum(TreeNode? root) {
88+
int maxi = -1000;
89+
List<TreeNode> stack = [];
90+
TreeNode? pre = root;
91+
TreeNode? cur = root;
92+
TreeNode? old = null;
93+
while (stack.isNotEmpty || cur != null) {
94+
while (cur != null) {
95+
stack.add(cur);
96+
cur = cur.left;
97+
}
98+
if (stack.isNotEmpty) {
99+
pre = stack.last;
100+
cur = pre.right;
101+
102+
if (cur == null || cur == old) {
103+
stack.removeLast();
104+
int left = pre.left == null ? 0 : pre.left!.val;
105+
int right = cur == null ? 0 : cur.val;
106+
107+
maxi = max(
108+
maxi,
109+
max(
110+
pre.val,
111+
max(pre.val + left,
112+
max(pre.val + right, pre.val + left + right))));
113+
pre.val = max(pre.val, max(pre.val + right, pre.val + left));
114+
115+
old = pre;
116+
cur = null;
117+
} else
118+
old = cur;
119+
}
120+
}
121+
return maxi;
122+
}
123+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
// Definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func max(a int, b int) int {
11+
if a > b {
12+
return a
13+
}
14+
return b
15+
}
16+
func min(a int, b int) int {
17+
if a < b {
18+
return a
19+
}
20+
return b
21+
}
22+
23+
// ==================== 1
24+
func maxPathSum(root *TreeNode) int {
25+
var maxi []int = make([]int, 1)
26+
maxi[0] = -1000
27+
pathDown(maxi, root)
28+
return maxi[0]
29+
}
30+
31+
func pathDown(maxi []int, root *TreeNode) int {
32+
if root == nil {
33+
return 0
34+
}
35+
var leftMax int = max(0, pathDown(maxi, root.Left))
36+
var rightMax int = max(0, pathDown(maxi, root.Right))
37+
maxi[0] = max(maxi[0], root.Val+leftMax+rightMax)
38+
return root.Val + max(leftMax, rightMax)
39+
}
40+
41+
// ================================== 2
42+
43+
// var val int = -1000
44+
45+
// func maxPathSum(root *TreeNode) int {
46+
// postOrder(root)
47+
// return val
48+
// }
49+
50+
// func postOrder(root *TreeNode) int {
51+
// if root == nil {
52+
// return 0
53+
// }
54+
// var sumLeft int = postOrder(root.Left)
55+
// var sumRight int = postOrder(root.Right)
56+
// var maxi int = max(root.Val, max(root.Val+sumLeft, max(root.Val+sumRight, root.Val+sumRight+sumLeft)))
57+
// if maxi > val {
58+
// val = maxi
59+
// }
60+
// // var value int = root.Val + max(sumLeft, sumRight)
61+
// if root.Val > root.Val+max(sumLeft, sumRight) {
62+
// return root.Val
63+
// } else {
64+
// return root.Val + max(sumLeft, sumRight)
65+
// }
66+
67+
// }
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 🔥 Binary Tree Maximum Path Sum 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Definition for a binary tree node
4+
5+
```dart
6+
class TreeNode {
7+
int val;
8+
TreeNode? left;
9+
TreeNode? right;
10+
TreeNode([this.val = 0, this.left, this.right]);
11+
}
12+
```
13+
14+
## Solution - 1 Post-Order Traversal
15+
16+
```dart
17+
class Solution {
18+
int val = -1000;
19+
int maxPathSum(TreeNode? root) {
20+
postOrder(root);
21+
return val;
22+
}
23+
24+
int postOrder(TreeNode? root) {
25+
if (root == null) return 0;
26+
int sumLeft = postOrder(root.left);
27+
int sumRight = postOrder(root.right);
28+
int maxi = max(
29+
root.val,
30+
max(root.val + sumLeft,
31+
max(root.val + sumRight, root.val + sumRight + sumLeft)));
32+
if (maxi > val) val = maxi;
33+
return root.val > root.val + max(sumLeft, sumRight)
34+
? root.val
35+
: root.val + max(sumLeft, sumRight);
36+
}
37+
}
38+
```
39+
40+
## Solution - 2
41+
42+
```dart
43+
class Solution {
44+
int maxPathSum(TreeNode? root) {
45+
List<int> maxi = [1];
46+
maxi[0] = -1000;
47+
pathDown(maxi, root);
48+
return maxi[0];
49+
}
50+
51+
int pathDown(List<int> maxi, TreeNode? root) {
52+
if (root == null) return 0;
53+
int leftMax = max(0, pathDown(maxi, root.left));
54+
int rightMax = max(0, pathDown(maxi, root.right));
55+
maxi[0] = max(maxi[0], root.val + leftMax + rightMax);
56+
return root.val + max(leftMax, rightMax);
57+
}
58+
}
59+
```
60+
61+
## Solution - 3 STACK
62+
63+
```dart
64+
class Solution {
65+
int maxPathSum(TreeNode? root) {
66+
int maxi = -1000;
67+
List<TreeNode> stack = [];
68+
TreeNode? pre = root;
69+
TreeNode? cur = root;
70+
TreeNode? old = null;
71+
while (stack.isNotEmpty || cur != null) {
72+
while (cur != null) {
73+
stack.add(cur);
74+
cur = cur.left;
75+
}
76+
if (stack.isNotEmpty) {
77+
pre = stack.last;
78+
cur = pre.right;
79+
80+
if (cur == null || cur == old) {
81+
stack.removeLast();
82+
int left = pre.left == null ? 0 : pre.left!.val;
83+
int right = cur == null ? 0 : cur.val;
84+
85+
maxi = max(
86+
maxi,
87+
max(
88+
pre.val,
89+
max(pre.val + left,
90+
max(pre.val + right, pre.val + left + right))));
91+
pre.val = max(pre.val, max(pre.val + right, pre.val + left));
92+
93+
old = pre;
94+
cur = null;
95+
} else
96+
old = cur;
97+
}
98+
}
99+
return maxi;
100+
}
101+
}
102+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
164164
- [**872.** Leaf-Similar Trees](LeafSimilarTrees/leaf_similar_trees.dart)
165165
- [**1026.** Maximum Difference Between Node and Ancestor](MaximumDifferenceBetweenNodeAndAncestor/maximum_difference_between_node_and_ancestor.dart)
166166
- [**1339.** Maximum Product of Splitted Binary Tree](MaximumProductOfSplittedBinaryTree/maximum_product_of_splitted_binary_tree.dart)
167+
- [**124.** Binary Tree Maximum Path Sum](BinaryTreeMaximumPathSum/binary_tree_maximum_path_sum.dart)
167168

168169
## Reach me via
169170

0 commit comments

Comments
 (0)