三种 js & ts 树遍历生成器 All In One
三种 js & ts 树遍历生成器 All In One
three js tree traversal generator / three ts tree traversal generator
二叉树 / 二叉搜索树
Binary Tree / Binary Search Tree
"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
*
*/
const log = console.log;
// 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);
}
}
export default TreeNode;
export {
TreeNode,
}
树遍历 / tree traversal
树
的遍历顺序取决于根
的遍历顺序
先序遍历 (根
1 => 左 => 右)
中序遍历 (左 => 根
2 => 右)
后序遍历 (左 => 右 => 根
3)
先序遍历
/ preorder traversal
DFS 深度优先搜索
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;
};
前序遍历
function preorderTraversal(root: TreeNode | null): number[] {
let result: number[] = [];
if(root !== null) {
// Preorder Traversal / 先序遍历
result.push(root.val)
// 接收 left 返回值
const left = preorderTraversal(root.left);
result = result.concat(left);
// 接收 right 返回值
const right = preorderTraversal(root.right);
result = result.concat(right);
}
return result;
};
中序遍历
/ inorder traversal
/*
// 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);
}
}
export default TreeNode;
export {
TreeNode,
}
*/
function inorderTraversal(root: TreeNode | null): number[] {
let result: number[] = [];
if(root !== null) {
// 接收 left 返回值
const left = inorderTraversal(root.left);
result = result.concat(left);
// Inorder Traversal / 中序遍历
result.push(root.val)
// 接收 right 返回值
const right = inorderTraversal(root.right);
result = result.concat(right);
}
return result;
};
// function inorderTraversal(root: TreeNode | null): number[] {
// let result: number[] = [];
// if(root !== null) {
// // console.log(`root =`, root, JSON.stringify(root));
// // {"val":1,"left":null,"right":{"val":2,"left":{"val":3,"left":null,"right":null},"right":null}}
// // console.log(`root =`, root, typeof root, Array.isArray(root));
// // root = [1,null,2,3] object false
// inorderTraversal(root.left);
// // Inorder Traversal / 中序遍历
// result.push(root.val)
// console.log(`root.val =`, root.val); ✅
// // root.val = 1
// // root.val = 3
// // root.val = 2
// inorderTraversal(root.right);
// }
// return result;
// };
后序遍历
/ postorder traversal
function postorderTraversal(root: TreeNode | null): number[] {
let result: number[] = [];
if(root !== null) {
// 接收 left 返回值
const left = postorderTraversal(root.left);
result = result.concat(left);
// 接收 right 返回值
const right = postorderTraversal(root.right);
result = result.concat(right);
// Postorder Traversal / 后序遍历
result.push(root.val)
}
return result;
};
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);
- preorder traversal tree generator / 先序遍历
// 中左右
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
// 先序遍历
result.push(head.val);
dfs(head.left, result)
dfs(head.right, result);
}
- inorder traversal tree generator / 中序遍历
// 左中右
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
dfs(head.left, result)
// 中序遍历
result.push(head.val);
dfs(head.right, result);
}
- postorder traversal tree generator / 后序遍历
// 左右中
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
dfs(head.left, result)
dfs(head.right, result);
// 后序遍历
result.push(head.val);
}
https://www.cnblogs.com/xgqfrms/p/16803069.html
LeetCode
树的遍历
https://leetcode.cn/leetbook/read/data-structure-binary-tree/xe17x7/
二叉树遍历
https://leetcode.com/problems/binary-tree-preorder-traversal/
https://leetcode.com/problems/binary-tree-inorder-traversal/
https://leetcode.com/problems/binary-tree-postorder-traversal/
https://leetcode.cn/problems/binary-tree-preorder-traversal/
https://leetcode.cn/problems/binary-tree-inorder-traversal/
https://leetcode.cn/problems/binary-tree-postorder-traversal/
LeetCode Playground & Binary Tree Traversal
// 手动生成 ✅ 非正常的二叉搜索树/排序二叉树
const node = new TreeNode(1);
node.right = new TreeNode(2);
node.right.left = new TreeNode(3);
console.log(`node =`, node);
function preorderTraversal(root: TreeNode | null): number[] {
if(!root) return [];
// 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;
};
const preorder = preorderTraversal(node);
console.log(`preorder tree =`, preorder);
function inorderTraversal(root: TreeNode | null): number[] {
if(!root) return [];
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
dfs(head.left, result)
// 中序遍历
result.push(head.val);
dfs(head.right, result);
}
let result: number[] = [];
dfs(root, result);
return result;
};
const tree = inorderTraversal(node);
console.log(`inorder tree =`, tree);
function postorderTraversal(root: TreeNode | null): number[] {
if(!root) return [];
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[]) {
if(!head) {
return;
}
dfs(head.left, result)
dfs(head.right, result);
// 后序遍历
result.push(head.val);
}
let result: number[] = [];
dfs(root, result);
return result;
};
const postorder = postorderTraversal(node);
console.log(`post tree =`, postorder);
https://leetcode.com/playground/7wyqA5aJ
https://www.cnblogs.com/xgqfrms/p/17052456.html#5141281
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/
demos
LeetCode 145.
Binary Tree Postorder Traversal
二叉树后序遍历
顺序: 左 => 右 => 根 (中) ✅
/**
* 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)
* }
* }
*/
// js tree generator
function postorderTraversal(root: TreeNode | null): number[] {
if(!root) {
return [];
}
let head = root;
let result: any[] = [];
// 二叉树后序遍历顺序: 左 => 右 => 根 (中) ✅
const dfs = (node: TreeNode | null, arr: any[]) => {
// 终止条件
if(!node) {
return;
}
// 先左
dfs(node.left, arr);
// 后右
dfs(node.right, arr);
// 最后根 (中)
if(node && node.val) {
arr.push(node.val);
}
// return arr;
}
dfs(head, result);
return result;
// return dfs(head, result);
};
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Inorder Traversal / 中序
遍历
Pre
order Traversal / 先序
遍历
Postorder
traversal / 后序
遍历
https://github.com/xgqfrms/daily-english-learning/issues/97
refs
https://www.cnblogs.com/xgqfrms/p/16803069.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17052456.html
未经授权禁止转载,违者必究!