Skip to content

Commit

Permalink
194th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 18, 2024
1 parent 60e0777 commit ec88181
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,18 @@ Ace Coding Interview with 75 Qs
[206]: ./src/page-2/206.%20Reverse%20Linked%20List/reverseList.ts
[2130]: ./src/page-20/2130.%20Maximum%20Twin%20Sum%20of%20a%20Linked%20List/pairSum.ts

| Binary Tree - DFS | | |
| -------------------------------------------- | --------------- | ------ |
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
| 872. Leaf-Similar Trees | [Solution][872] | Easy |
| 1448. Count Good Nodes in Binary Tree | Solution | Medium |
| 437. Path Sum III | Solution | Medium |
| 1372. Longest ZigZag Path in a Binary Tree | Solution | Medium |
| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium |
| Binary Tree - DFS | | |
| -------------------------------------------- | ---------------- | ------ |
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
| 872. Leaf-Similar Trees | [Solution][872] | Easy |
| 1448. Count Good Nodes in Binary Tree | [Solution][1448] | Medium |
| 437. Path Sum III | Solution | Medium |
| 1372. Longest ZigZag Path in a Binary Tree | Solution | Medium |
| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium |

[104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts
[872]: ./src/page-9/872.%20Leaf-Similar%20Trees/leafSimilar.ts
[1448]: ./src/page-14/1448.%20Count%20Good%20Nodes%20in%20Binary%20Tree/goodNodes.ts

| Binary Tree - BFS | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateBinaryTree } from '~/utils/binary-tree';

import { goodNodes } from './goodNodes';

describe('1448. Count Good Nodes in Binary Tree', () => {
test('goodNodes', () => {
{
const root = generateBinaryTree([3, 1, 4, 3, null, 1, 5]);
expect(goodNodes(root)).toBe(4);
}

{
const root = generateBinaryTree([3, 3, null, 4, 2]);
expect(goodNodes(root)).toBe(3);
}

{
const root = generateBinaryTree([1]);
expect(goodNodes(root)).toBe(1);
}
});
});
29 changes: 29 additions & 0 deletions src/page-14/1448. Count Good Nodes in Binary Tree/goodNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { TreeNode } from '~/utils/binary-tree';

type GoodNodes = (root: TreeNode | null) => number;

/**
* Accepted
*/
export const goodNodes: GoodNodes = (root) => {
// Helper function to perform DFS
function dfs(node: TreeNode | null, maxVal: number): number {
if (node === null) return 0;

// Determine if the current node is a "good" node
let count = 0;
if (node.val >= maxVal) count = 1;

// Update maxVal for the next recursion
const newMaxVal = Math.max(maxVal, node.val);

// Continue DFS on left and right children
count += dfs(node.left, newMaxVal);
count += dfs(node.right, newMaxVal);

return count;
}

// Start DFS with the root node
return dfs(root, root?.val ?? Number.NEGATIVE_INFINITY);
};

0 comments on commit ec88181

Please sign in to comment.