It's possible to create a function auto generator this special test case binary tree from array in javascript?
It's possible to create a function auto generator this special test case binary tree from array in javascript?
I want to auto generate those test cases in my local env. I'm not asking the LeetCode problem's answer, please read my question first!
situation
generator the
special test case
binary tree for local testing, which isn't asearch binary tree
;
I have an array of nodes of a binary tree and wanted to create a special binary tree like the below image shows.
// nodes array
const root = [3,9,20,null,null,15,7]
I tried a few things and searched google, still can't find a solution;
is there any way to find a solution to this problem, please?
PS: I know that I can
manually
create it.
// created by manually
const specialTestCaseTree = {
val: 3,
left: {
val: 9,
left: null,
right: null,
},
right: {
val: 20,
left: {
val: 15,
left: null,
right: null,
},
right: {
val: 7,
left: null,
right: null,
},
},
};
try
class TreeNode {
constructor(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
}
class BinaryTreeGenerator {
constructor() {
this.root = null;
this.flag = `left`;
}
insert(key) {
if(this.root === null) {
this.root = new TreeNode(key);
} else {
this.insertNode(this.root, key)
}
}
insertNode(node, key) {
// ignore null
if(key === null) {
return;
}
// tree traversal(root=>left=>right)
if(this.flag === `left`) {
// left
if(node.left === null) {
node.left = new TreeNode(key);
this.flag = `right`;
} else {
this.insertNode(node.left, key)
}
} else {
// right
if(node.right === null) {
node.right = new TreeNode(key);
this.flag = `left`;
} else {
this.insertNode(node.right, key)
}
}
}
}
// testing
const tree = new BinaryTreeGenerator();
const arr = [3,9,20,null,null,15,7];
for (let key of arr) {
tree.insert(key)
}
console.log(`tree =`, tree);
// ❌ not the wanted
LeetCode 二叉树生成器 / Binary Tree Generator
ChatGPT ✅
class TreeNode {
constructor(val) {
this.val = val;
this.left = this.right = null;
}
}
function createBinaryTree(arr, i = 0) {
if (i >= arr.length || arr[i] === null) return null;
let node = new TreeNode(arr[i]);
node.left = createBinaryTree(arr, 2 * i + 1);
node.right = createBinaryTree(arr, 2 * i + 2);
return node;
}
const arr = [3, 9, 20, null, null, 15, 7];
const root = createBinaryTree(arr);
console.log(root);
https://leetcode.com/problems/maximum-depth-of-binary-tree/
references
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
Fuck 小日本
LeetCode & tree
- Binary Tree Paths / 257. 二叉树路径
https://leetcode.com/problems/binary-tree-paths/description/
- Flip Equivalent Binary Trees / 951. 翻转等价二叉树
https://leetcode.com/problems/flip-equivalent-binary-trees/
refs
https://stackoverflow.com/questions/73202858/test-leetcode-104-maximum-depth-of-binary-tree
https://stackoverflow.com/questions/48744012/how-to-make-binary-tree-from-array-in-javascript
https://stackoverflow.com/questions/60175433/flip-equivalent-binary-trees-in-leetcode
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17080136.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-01-31 js ?. operator All In One
2021-01-31 Shell 编程快速上手
2021-01-31 uni-app in action
2021-01-31 互联网公司专用术语汇总 All In One
2020-01-31 2020 新型肺炎病毒疫情 & 远程办公
2020-01-31 H5 & animation