Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion Good Nodes for BTree 1432412
(version: 0)
Comparing performance of:
first vs second vs third vs forth
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class TreeNode { constructor(val, left, right) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function goodNodes(root, max = Number.MIN_SAFE_INTEGER) { if (!root) return 0; let result = root.val >= max ? 1 : 0; result += goodNodes(root.left, Math.max(max, root.val)); result += goodNodes(root.right, Math.max(max, root.val)); return result; } function buildTreeFromArray(arr) { if (!arr.length || arr[0] === null) return null; const nodes: (TreeNode | null)[] = arr.map((val) => val !== null ? new TreeNode(val) : null ); for (let i = 0; i < nodes.length; i++) { if (nodes[i] !== null) { const leftIndex = 2 * i + 1; const rightIndex = 2 * i + 2; const curr = nodes[i] as TreeNode; if (leftIndex < nodes.length) { curr.left = nodes[leftIndex]; } if (rightIndex < nodes.length) { curr.right = nodes[rightIndex]; } } } return nodes[0]; }
Tests:
first
const input = [3, 1, 4, 3, null, 1, 5]; const tree = buildTreeFromArray(input); goodNodes(tree)
second
const input = [3,3,null,4,2]; const tree = buildTreeFromArray(input); goodNodes(tree)
third
const input = [1]; const tree = buildTreeFromArray(input); goodNodes(tree)
forth
const input = [3, 2, 5, 3, 4, 8, 1, 6, null, null, null, 7, null, 13, 9]; const tree = buildTreeFromArray(input); goodNodes(tree)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
first
second
third
forth
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Comments
Confirm delete:
Do you really want to delete benchmark?