JavaScript 数据结构——二叉树

概念

二叉树是树中每个节点最多只能有两个子节点的

实现

在 JavaScript 中常用Object来实现二叉树,如上图二叉树可表示为:

const bt = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null,
        },
        right: {
            val: 5,
            left: {
                val: 8,
                left: null,
                right: null,
            },
            right: {
                val: 9,
                left: null,
                right: null,
            },
        },
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null,
        },
        right: {
            val: 7,
            left: null,
            right: null,
        },
    },
};

遍历

先序遍历

  1. 访问根节点
  2. 对根节点的左子树进行先序遍历
  3. 对根节点的右子树进行先序遍历

递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 递归先序遍历: 根 - 左 - 右
const preorderTraversal = root => {
    const res = [];
    const preOrder = root => {
        if (!root) return;
        res.push(root.val); // 根
        preOrder(root.left); // 左
        preOrder(root.right); // 右
    };
    preOrder(root);
    return res;
};

非递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 先序遍历(非递归)
const preorderTraversal = root => {
    if (!root) return [];
    const res = [];
    const stack = [root];
    while (stack.length) {
        // n是栈顶元素,栈顶元素出栈
        const n = stack.pop();
        // 访问栈顶元素
        res.push(n.val);
        // 栈顶左节点入栈
        n.right && stack.push(n.right);
        // 栈顶右节点入栈
        n.left && stack.push(n.left);
    }
    return res;
};

中序遍历

  1. 对根节点的左子树进行先序遍历
  2. 访问根节点
  3. 对根节点的右子树进行先序遍历

递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 递归中序遍历:左-根-右
const inorderTraversal = root => {
    const res = [];
    const inOrder = root => {
        if (!root) return;
        inOrder(root.left); // 左
        res.push(root.val); // 根
        inOrder(root.right); // 右
    };
    inOrder(root);
    return res;
};

非递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 中序遍历(非递归)
const inorderTraversal = root => {
    if (!root) return [];
    const res = [];
    const stack = [];
    let p = root;
    while (stack.length || p) {
        while (p) {
            stack.push(p);
            p = p.left;
        }
        const n = stack.pop();
        res.push(n.val);
        p = n.right;
    }
    return res;
};

后序遍历

  1. 对根节点的左子树进行先序遍历
  2. 对根节点的右子树进行先序遍历
  3. 访问根节点

递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 递归后序遍历:左-右-根
const postorderTraversal = root => {
    const res = [];
    const postOrder = root => {
        if (!root) return;
        postOrder(root.left); // 左
        postOrder(root.right); // 右
        res.push(root.val); // 根
    };
    postOrder(root);
    return res;
};

非递归版:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

// 后序遍历(非递归)
const postorderTraversal = root => {
    if (!root) return [];
    const arr = [root];
    const res = [];
    while (arr.length) {
        const n = arr.pop();
        res.unshift(n.val);
        n.left && arr.push(n.left);
        n.right && arr.push(n.right);
    }
    return res;
};
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火星飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值