JS sequence construction of binary tree, JS sequence traversal of binary tree, construction of binary tree sequence array of JS force button

Table of contents

1. Example

2. Sequence construction of binary tree

JavaScript

TypeScript

3. Layer sequence traversal binary tree iteration method

JavaScript

TypeScript

4. Sequential traversal of the binary tree: keep the null version


        The new article has been published, based on the binary tree to create dom display on the page  JS display the binary tree on the page, binary tree to DOM_l pancake fruit blog-CSDN blog

1. Example

        When using the Likou algorithm, the binary tree examples given are all in the form of arrays. If you want to write code on your own compiler, you need to convert the array into a real binary tree, so that it can be used as an input example. Even if the array contains null, it can be automatically skipped without creating nodes (see the example below)

Initial array:

After construction: 

After layer order traversal:

2. Sequence construction of binary tree

The queue is used to continuously take out nodes and put in new nodes until each node is assigned a child node. When there is null in the array, no new node will be created (that is, skip this null)

JavaScript

/**
 * 二叉树节点类,与Leecode的定义方法相同
 * val: number  当前节点值
 * left: TreeNode 左节点
 * right: TreeNode 右节点
 */
class TreeNode {
    val;
    left;
    right;
    constructor(val, left, right) {
        this.val = (val === undefined ? 0 : val);
        this.left = (left === undefined ? null : left);
        this.right = (right === undefined ? null : right);
    }
}
/**
 * 根据层序数组构造二叉树 ,比如 [1,2,3] 变为  根1 左2 右3
 * @param arr 传入的数组
 * @returns TreeNode 这个树的根节点
 */
const buildTree = (arr) => {
    let i = 0; //i每次用完都需要自增1,因为层序构造依赖于数组的索引
    let root = new TreeNode(arr[i++]);
    let NodeList = [root];
    while (NodeList.length) {
        let node = NodeList.shift();
        if (arr[i] !== null) { //如果是空的就不创建节点
            node.left = new TreeNode(arr[i]); //创建左节点
            NodeList.push(node.left);
        }
        i++; //不管是不是空的,i都需要自增
        if (i == arr.length)
            return root; //如果长度已经够了就返回,免得数组索引溢出
        if (arr[i] !== null) { //如果是空的就不创建节点
            node.right = new TreeNode(arr[i]); //创建右节点
            NodeList.push(node.right);
        }
        i++; //不管是不是空的,i都需要自增
        if (i == arr.length)
            return root; //如果长度已经够了就返回,免得数组索引溢出
    }
    return root;
};
const root1 = buildTree([4, 2, 7, 1, 3]);
console.log(root1);

TypeScript

/**
 * 二叉树节点类,与Leecode的定义方法相同
 * val: number  当前节点值
 * left: TreeNode 左节点
 * right: TreeNode 右节点
 */
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)
  }
}

/**
 * 根据层序数组构造二叉树 ,比如 [1,2,3] 变为  根1 左2 右3
 * @param arr 传入的数组
 * @returns TreeNode 这个树的根节点
 */
const buildTree = (arr: number[]): TreeNode => {
    let i: number = 0 //i每次用完都需要自增1,因为层序构造依赖于数组的索引
  let root: TreeNode = new TreeNode(arr[i++])
  let NodeList: TreeNode[] = [root]
  while (NodeList.length) {
    let node: TreeNode = NodeList.shift()
    if (arr[i] !== null) {//如果是空的就不创建节点
      node.left = new TreeNode(arr[i]) //创建左节点
      NodeList.push(node.left)
    }
    i++//不管是不是空的,i都需要自增
    if (i == arr.length) return root //如果长度已经够了就返回,免得数组索引溢出
    if (arr[i] !== null) {//如果是空的就不创建节点
      node.right = new TreeNode(arr[i]) //创建右节点
      NodeList.push(node.right)
    }
    i++//不管是不是空的,i都需要自增
    if (i == arr.length) return root //如果长度已经够了就返回,免得数组索引溢出
  }
  return root
};
const root1: TreeNode = buildTree([4, 2, 7, 1, 3]);
console.log(root1);

3. Layer sequence traversal binary tree iteration method

Regarding the definition of the binary tree node class TreeNode, in the above code there is

JavaScript

function func(root) {
    let res = [];
    let queue = []; //辅助队列
    if (root)
        queue.push(root); //根节点不为空才放入
    while (queue.length) {
        let len = queue.length; //需要固定长度,因为后面会不断在队列中添加和取出数据
        for (let i = 0; i < len; i++) {
            let node = queue.shift(); //从队列头部取出
            res.push(node.val); //放入结果数组
            if (node.left)
                queue.push(node.left);
            if (node.right)
                queue.push(node.right);
        }
    }
    return res;
}

TypeScript

function func(root: TreeNode): number[] {
  let res: number[] = []
  let queue: TreeNode[] = []//辅助队列
  if (root) queue.push(root)//根节点不为空才放入
  while (queue.length) {
    let len = queue.length //需要固定长度,因为后面会不断在队列中添加和取出数据
    for (let i = 0; i < len; i++) {
      let node: TreeNode = queue.shift() //从队列头部取出
      res.push(node.val) //放入结果数组
      if (node.left) queue.push(node.left)
      if (node.right) queue.push(node.right)
    }
  }
  return res
}

4. Sequential traversal of the binary tree: keep the null version

        This version will keep empty nodes and put them into the result array as null to realize: what data is input to generate a binary tree, what is the data traversed

/** 层序遍历二叉树,输出数组   特化版:保留null */
const outTree = (root) => {
    let res = [];
    let queue = [];
    if (root) {
        res.push(root.val);
        queue.push(root);
    }
    let len;
    while (queue.length) {
        len = queue.length;
        // console.log(JSON.parse(JSON.stringify(queue)));
        for (let i = 0; i < len; i++) {
            let node = queue.shift();
            if (node.left)
                queue.push(node.left);
            res.push(node.left?.val || null); //当前节点的左边没数值就放入null,否则放入左节点值
            if (node.right)
                queue.push(node.right);
            res.push(node.right?.val || null); //当前节点的右边没数值就放入null,否则放入右节点值
        }
    }
    // 最后一层的左右子树虽为空,但是也放入了null,需要删除数组末尾无用的null
    // 方法一: 只要是末尾的null都剔除
    for (let i = res.length - 1; i >= 0; i--) {
        if (res[i] === null)
            res.length--; //遇到null就长度-1
        else
            break; //一旦遇到了不是null的,就立马退出
    }
    //方法二:根据最后一层的节点个数,剔除 len * 2个null
    // res.length -= len * 2 //这个会保留最后一层的null,比如[50, 30, 70, 8, 40, 60],在最后一层其实是 8 40 60 null,这个方法会保留这个null,上面的不会
    return res;
};

Guess you like

Origin blog.csdn.net/m0_64130892/article/details/130109558