xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js Tree Data Structure & Tree Traversal Algorithm All In One

js Tree Data Structure & Tree Traversal Algorithm All In One

树的数据结构和算法

生成树和节点

/**
 * 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)
 * }
 */

function TreeNode(val, left, right) {
  this.val = (val===undefined ? 0 : val)
  this.left = (left===undefined ? null : left)
  this.right = (right===undefined ? null : right)
}


生成链表和节点


/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */


function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}

树/二叉树



tree traversal / 树遍历

root = [];

  1. 前序遍历

前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树。
前序遍历的结果是:1, 2, 4, 5, 7, 8, 3, 6

  1. 中序遍历

中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。
中序遍历的结果是:4, 2, 7, 5, 8, 1, 3, 6

  1. 后序遍历

后序遍历是先遍历左子树,然后遍历右子树,最后访问树的根节点。
后序遍历的结果是:4, 7, 8, 5, 2, 6, 3, 1

// 前序遍历
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
    let result=[];
    // DFS 深度优先算法 
    (function traversalTreeNode(root){
        if(root) {
            result.push(root.val);
       }
        if(root&&root.left) {
            //递归
            traversalTreeNode(root.left);
        }
        if(root&&root.right) {
            //递归
            traversalTreeNode(root.right);
        }
    })(root);
    return result;
};



/**
 * @param {TreeNode} root
 * @return {arr[]}
 */
var preorderTraversal = function (root) {
    let stack = [root];
    let arr = [];
    // BFS ???
    while (stack.length > 0) {
        //循环迭代
        let node = stack.pop();
        node && arr.push(node.val); 
        // node不为空时,向arr中推入节点值
        node && node.right && stack.push(node.right); 
        //关键点:模拟栈,后入先出,故先压右节点
        node && node.left && stack.push(node.left); 
        // 关键点:后入先出,后压左节点
    }
    return arr;
};


// 中序遍历

// 后序遍历

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    // 二分搜索算法
    if(!nums.length) {
      return -1;
    }
    let left = 0;
    let right = nums.length -1;
    while(left <= right) {
      let mid = Math.floor((left + right) / 2);
      if(nums[mid] === target) {
        return mid;
      }
      if(nums[mid] > target) {
        right = mid - 1;
      }
      if(nums[mid] < target) {
        left = mid + 1;
      }
    }
    return -1;
};

demo


https://leetcode-cn.com/study-plan/data-structures/

https://leetcode.com/explore/interview/

https://leetcode.com/explore/featured/

https://leetcode.com/explore/learn/card/binary-search/

refs

https://githubhelp.com/loiane/javascript-datastructures-algorithms

https://github.com/loiane/javascript-datastructures-algorithms

https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/js

https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/ts

https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/ts/data-structures



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-02-08 22:27  xgqfrms  阅读(33)  评论(2编辑  收藏  举报