JavaScript Data Structure: Binary Tree & tree generator All In One
JavaScript Data Structure: Binary Tree & tree generator All In One
js binary tree generator
Binary Tree Generator / 二叉树生成器
treeGenerator
binary-tree-generator.ts
class TreeNode {
public val: TreeNode;
public left: TreeNode | null;
public right: TreeNode | null;
constructor(value?) {
this.val = value ?? null;
this.left = null;
this.right = null;
}
}
// 二叉搜索树 / 排序二叉树
class BinaryTreeGenerator {
public root;
constructor() {
this.root = null;
}
insert (key) {
if(this.root === null) {
this.root = new TreeNode(key);
} else {
this.insertNode(this.root, key)
}
}
insertNode (node, key) {
if(key < node.val) {
// left
if(node.left === null) {
node.left = new TreeNode(key);
} else {
this.insertNode(node.left, key)
}
} else {
// right
if(node.right === null) {
node.right = new TreeNode(key);
} else {
this.insertNode(node.right, key)
}
}
}
// root
// insert
// search
// traversal ...
// preTraversal ...
// InorderTraversal ...
// postTraversal ...
// min
// max
}
binary-tree-generator.js
class TreeNode {
constructor(value) {
this.val = value;
this.left = null;
this.right = null;
}
}
// 二叉搜索树 / 排序二叉树
class BinaryTreeGenerator {
constructor() {
this.root = null;
}
insert (key) {
if(this.root === null) {
this.root = new TreeNode(key);
} else {
this.insertNode(this.root, key)
}
}
insertNode (node, key) {
if(key < node.val) {
// left
if(node.left === null) {
node.left = new TreeNode(key);
} else {
this.insertNode(node.left, key)
}
} else {
// right
if(node.right === null) {
node.right = new TreeNode(key);
} else {
this.insertNode(node.right, key)
}
}
}
}
// test
const tree = new BinaryTreeGenerator();
const keys = [1,null,2,3];
for (const key of keys) {
tree.insert(key);
}
console.log(`tree = `, tree);
console.log(`tree.root = `, tree.root);
树的遍历
https://leetcode.cn/leetbook/read/data-structure-binary-tree/xe17x7/
// Definition for a binary tree node.
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
}
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-10-10
* @modified
*
* @description树 & 遍历
* @difficulty Easy
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://leetcode.cn/leetbook/read/data-structure-binary-tree/xe17x7/
* @solutions
*
* @best_solutions
*
*/
// export {};
const log = console.log;
// Definition for a binary tree node.
class TreeNode {
constructor(val , left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
}
// bug ❌
const treeGenerator = (list) => {
if(!list.length) return [];
// let tree = new TreeNode(list[0]);
let tree = new TreeNode();
let temp = tree;
console.log(`tree =`, tree);
for (const [i, item] of list.entries()) {
// console.log(`item =`, item);
// if(i === 0) {
// continue;
// }
temp.val = item;
console.log(`temp 1 =`, temp);
if(item !== null) {
temp.right = new TreeNode(null);
temp.left = new TreeNode(list[i + 1]);
if(i < list.length - 1) {
temp = temp.left;
}
} else {
temp.left = new TreeNode(null);
temp.right = new TreeNode(list[i + 1]);
if(i < list.length - 1) {
temp = temp.right;
}
}
console.log(`temp 2 =`, temp);
}
console.log(`tree ?=`, tree);
return tree;
}
// test
treeGenerator([1,null,2,3]);
$ npx ts-node ./tree-generator.ts
https://leetcode.cn/leetbook/read/data-structure-binary-tree/xecaj6/
LeetCode
function preOrderTraversal(root: TreeNode | null): number[] {
if(!root) return [];
// 144. 二叉树的前序遍历: root, left, right
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
result.push(head.val);
dfs(head.left, result)
dfs(head.right, result);
}
let result: number[] = [];
dfs(root, result);
return result;
};
https://leetcode.com/problems/binary-tree-preorder-traversal/
https://leetcode.cn/problems/binary-tree-preorder-traversal/
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/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
js binary tree generator / js 二叉树生成器
https://www.cnblogs.com/xgqfrms/p/17052456.html
/Users/xgqfrms-mm/Documents/github/leetcode/js-solutions/144 binary-tree-preorder-traversal.ts
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16803069.html
未经授权禁止转载,违者必究!