Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iteration 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) { if (!root) return 0; const stack = [root]; const max = [root.val]; let count = 1; while (stack.length > 0) { const curr = stack.pop(); const currMax = max.pop(); if (curr.left) { const left = curr.left; if (left.val >= currMax) { count += 1; max.unshift(left.val); } else { max.unshift(currMax); } stack.unshift(left); } if (curr.right) { const right = curr.right; if (right.val >= currMax) { count += 1; max.unshift(right.val); } else { max.unshift(currMax); } stack.unshift(right); } } return count; } 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?