Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30f0272

Browse files
Binary tree maximum path sum
1 parent 401764d commit 30f0272

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

‎0124_binaryTreeMaximumPathSum.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @typedef {Object} TreeNode
3+
* @description Definition for a binary tree node.
4+
* function TreeNode(val) {
5+
* this.val = val;
6+
* this.left = this.right = null;
7+
* }
8+
*/
9+
10+
let max = Number.MIN_SAFE_INTEGER;
11+
12+
const maxValue = node => {
13+
if (node === null) return 0;
14+
15+
let leftValue = Math.max(maxValue(node.left), 0);
16+
let rightValue = Math.max(maxValue(node.right), 0);
17+
18+
max = Math.max(max, node.val + leftValue + rightValue);
19+
20+
return node.val + Math.max(leftValue, rightValue);
21+
};
22+
23+
/**
24+
* @param {TreeNode} root Head of the binary tree.
25+
* @return {max} Maximum path sum.
26+
* @summary Binary Tree Maximum Path Sum {@link https://leetcode.com/problems/binary-tree-maximum-path-sum/}
27+
* @description Given a non-empty binary tree, find the maximum path sum
28+
* Space O(n) - where n is number of nodes.
29+
* Time O(k) - where k is height of tree.
30+
*/
31+
const maxPathSum = root => {
32+
maxValue(root);
33+
34+
return max;
35+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @typedef {Object} TreeNode
3+
* @description Definition for a binary tree node.
4+
* function TreeNode(val) {
5+
* this.val = val;
6+
* this.left = this.right = null;
7+
* }
8+
*/
9+
10+
/**
11+
* @summary Serialize and Deserialize Binary Tree {@link https://leetcode.com/problems/serialize-and-deserialize-binary-tree/solution/}
12+
* @description Write functions to serialize and deserialize binary tree.
13+
* Space O(n) - For both serialize and deserialize, we store every node.
14+
* Time O(n) - For both serialize and deserialize, we traverse every node.
15+
*/
16+
17+
/**
18+
* Encodes a tree to a single string.
19+
*
20+
* @param {TreeNode} root
21+
* @return {string}
22+
*/
23+
const serialize = root => {
24+
const queue = [root];
25+
const result = [];
26+
27+
while (queue.length) {
28+
const current = queue.shift();
29+
30+
if (current === null) result.push('null');
31+
else {
32+
result.push(current.val);
33+
34+
queue.push(current.left);
35+
queue.push(current.right);
36+
}
37+
}
38+
39+
while (result[result.length - 1] === 'null') {
40+
result.pop();
41+
}
42+
43+
return result.join(',');
44+
};
45+
46+
/**
47+
* Decodes your encoded data to tree.
48+
*
49+
* @param {string} data
50+
* @return {TreeNode}
51+
*/
52+
const deserialize = string => {
53+
if (string === '') return null;
54+
55+
const treeData = string.split(',');
56+
57+
const root = new TreeNode(+treeData.shift());
58+
const queue = [root];
59+
60+
while (queue.length) {
61+
const current = queue.shift();
62+
63+
if (treeData.length) {
64+
const leftData = treeData.shift();
65+
66+
if (leftData !== 'null') {
67+
const left = new TreeNode(+leftData);
68+
current.left = left;
69+
queue.push(current.left);
70+
}
71+
}
72+
73+
if (treeData.length) {
74+
const rightData = treeData.shift();
75+
76+
if (rightData !== 'null') {
77+
const right = new TreeNode(+rightData);
78+
current.right = right;
79+
queue.push(current.right);
80+
}
81+
}
82+
}
83+
return root;
84+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /