Skip to content

Commit 10165a7

Browse files
committed
Solve LeetCode 75 binary tree DFS problems
1 parent d9fc8f3 commit 10165a7

File tree

7 files changed

+239
-6
lines changed

7 files changed

+239
-6
lines changed

LeetCode-75/33-104.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/maximum-depth-of-binary-tree/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstMaxDepth = (root: TreeNode | null, depth: number): number => {
19+
if (root === null) return depth;
20+
21+
const leftDepth = depthFirstMaxDepth(root.left, depth + 1);
22+
const rightDepth = depthFirstMaxDepth(root.right, depth + 1);
23+
return Math.max(leftDepth, rightDepth);
24+
};
25+
26+
const maxDepth = (root: TreeNode | null): number => depthFirstMaxDepth(root, 0);

LeetCode-75/34-872.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/leaf-similar-trees/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstLeafSequence = (root: TreeNode | null): number[] => {
19+
if (root === null) return [];
20+
if (root.left === null && root.right === null) return [root.val];
21+
22+
const leftVals = depthFirstLeafSequence(root.left);
23+
const rightVal = depthFirstLeafSequence(root.right);
24+
return [...leftVals, ...rightVal];
25+
};
26+
27+
const leafSimilar = (root1: TreeNode | null, root2: TreeNode | null): boolean =>
28+
depthFirstLeafSequence(root1).join(",") ===
29+
depthFirstLeafSequence(root2).join(",");

LeetCode-75/35-1448.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/path-sum-iii/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstGoodCount = (root: TreeNode | null, max: number): number => {
19+
if (root === null) return 0;
20+
21+
max = Math.max(root.val, max);
22+
23+
return (
24+
(root.val === max ? 1 : 0) +
25+
depthFirstGoodCount(root.left, max) +
26+
depthFirstGoodCount(root.right, max)
27+
);
28+
};
29+
30+
const goodNodes = (root: TreeNode | null): number =>
31+
depthFirstGoodCount(root, root.val);

LeetCode-75/36-437.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// https://leetcode.com/problems/path-sum-iii/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstPathSumCount = (
19+
root: TreeNode | null,
20+
targetSum: number,
21+
pathSumMap: Map<TreeNode, number>
22+
): number[] => {
23+
if (root === null) return [0, 0];
24+
25+
pathSumMap.set(root, root.val);
26+
27+
let localPathSumCount = 0;
28+
29+
pathSumMap.forEach((val, key) => {
30+
if (key !== root) val += root.val;
31+
if (val === targetSum) localPathSumCount++;
32+
pathSumMap.set(key, val);
33+
});
34+
35+
const [leftTreePathSumCount, leftChildVal] = depthFirstPathSumCount(
36+
root.left,
37+
targetSum,
38+
pathSumMap
39+
);
40+
41+
if (leftChildVal)
42+
pathSumMap.forEach((val, key) => pathSumMap.set(key, val - leftChildVal));
43+
44+
const [rightTreePathSumCount, rightChildVal] = depthFirstPathSumCount(
45+
root.right,
46+
targetSum,
47+
pathSumMap
48+
);
49+
50+
pathSumMap.delete(root);
51+
52+
if (rightChildVal)
53+
pathSumMap.forEach((val, key) => pathSumMap.set(key, val - rightChildVal));
54+
55+
return [
56+
localPathSumCount + leftTreePathSumCount + rightTreePathSumCount,
57+
root.val,
58+
];
59+
};
60+
61+
const pathSum = (root: TreeNode | null, targetSum: number): number =>
62+
depthFirstPathSumCount(root, targetSum, new Map())[0];

LeetCode-75/37-1372.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstLongestZigZagPath = (
19+
root: TreeNode | null,
20+
direction: "L" | "R" | null
21+
): { currMax: number; max: number } => {
22+
if (root === null) return { currMax: 0, max: 0 };
23+
24+
const rightRes = depthFirstLongestZigZagPath(root.right, "R");
25+
const leftRes = depthFirstLongestZigZagPath(root.left, "L");
26+
27+
const max = Math.max(rightRes.max, leftRes.max);
28+
29+
if (direction === "L") {
30+
const currMax = rightRes.currMax + 1;
31+
return { currMax, max: Math.max(currMax, max) };
32+
} else if (direction === "R") {
33+
const currMax = leftRes.currMax + 1;
34+
return { currMax, max: Math.max(currMax, max) };
35+
} else {
36+
return { currMax: max, max };
37+
}
38+
};
39+
40+
const longestZigZag = (root: TreeNode | null): number =>
41+
depthFirstLongestZigZagPath(root, null).max;

LeetCode-75/38-236.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const depthFirstLowestCommonAncestor = (
19+
root: TreeNode | null,
20+
p: TreeNode | null,
21+
q: TreeNode | null
22+
): TreeNode[] => {
23+
if (root === null) return [];
24+
25+
const res = [
26+
...depthFirstLowestCommonAncestor(root.left, p, q),
27+
...depthFirstLowestCommonAncestor(root.right, p, q),
28+
];
29+
30+
if (res.length === 3) return res;
31+
32+
if (res.length === 2 || root === p || root === q) {
33+
res.push(root);
34+
if (res.length === 2) res.push(root);
35+
}
36+
37+
return res;
38+
};
39+
40+
const lowestCommonAncestor = (
41+
root: TreeNode | null,
42+
p: TreeNode | null,
43+
q: TreeNode | null
44+
): TreeNode | null => depthFirstLowestCommonAncestor(root, p, q)[2];

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060

6161
### Binary Tree - DFS
6262

63-
- [ ] 33. Maximum Depth of Binary Tree
64-
- [ ] 34. Leaf-Similar Trees
65-
- [ ] 35. Count Good Nodes in Binary Tree
66-
- [ ] 36. Path Sum III
67-
- [ ] 37. Longest ZigZag Path in a Binary Tree
68-
- [ ] 38. Lowest Common Ancestor of a Binary Tree
63+
- [X] 33. Maximum Depth of Binary Tree
64+
- [X] 34. Leaf-Similar Trees
65+
- [X] 35. Count Good Nodes in Binary Tree
66+
- [X] 36. Path Sum III
67+
- [X] 37. Longest ZigZag Path in a Binary Tree
68+
- [X] 38. Lowest Common Ancestor of a Binary Tree
6969

7070
### Binary Tree - BFS
7171

0 commit comments

Comments
 (0)