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

js binary tree All In One

js binary tree All In One

二叉树 / binary tree

前序遍历:根节点、左节点、右节点(1、2、4、5、7、8、3、6)
中序遍历:左节点、根节点、右节点(4、2、7、5、8、1、3、6)
后序遍历:左节点、右节点、根节点(4、7、8、5、2、6、3、1)



tree

概念

节点: 根节点, 子节点, 外部节点(叶子节点), 内部节点();

层次: 根节点 0 层, (1 ~ n) 层;

深度: 叶子节点的祖先节点的数量, 即(节点层次 - 1; 或者祖先节点与叶子节点的边数);

高度: 最大的叶子节点的深度, 即(最大的节点层次 - 1; 或者根节点与叶子节点的最大边数);

边: 两个节点之间的连线

树: 一系列存在父子关系的节点, 除根节点外, 每个节点都有 1 个父节点, 和大于等于 0 个的子节点;
子树: 把一个内部节点作为根节点的树, 即(内部节点和其后代子节点)

二叉树: 每个节点上的子节点数量小于等于 2 个(左节点, 右节点)

平衡二叉树:
完全二叉树:
满二叉树:

二叉搜索树: 每个父节点的左子节点值都小于父节点的值, 右子节点值都大于等于父节点的值, 即(左节点值 < 父节点的值 <= 右子节点值)

  1. BST BinarySearchTree

树遍历

  1. 中序遍历: 从小到大,顺序遍历,左中右
  2. 先序遍历: 先父节点,后子节点,中左右
  3. 后序遍历: 先子节点,后父节点,左右中


112. Path Sum

  1. 路径总和

https://leetcode.com/problems/path-sum/
https://leetcode-cn.com/problems/path-sum

  1. DFS 深度优先算法

/**
 * 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)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function(root, targetSum) {
  if(!root) {
    // no exist root node
    return false;
  }
  if(!root.left && !root.right) {
     // is leaf node
    return targetSum === root.val;
  } else {
    const sum = targetSum - root.val;
    // DFS 深度优先算法
    return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
  } 
};

/**
 * 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)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function(root, targetSum) {
  if(!root) {
    // no exist root node
    return false;
  }
  if(!root.left && !root.right) {
     // is leaf node
    return targetSum === root.val;
  } else {
    // DFS 深度优先算法
    return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
  } 
};

  1. BFS 广度优先算法

/**
 * 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)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function(root, targetSum) {
  if(!root) {
    // no exist root node
    return false;
  }
  // BFS 广度优先算法
  const nodeQueue = [root];
  const sumQueue = [root.val];
  // 队列不空
  while(nodeQueue.length) {
     let node = nodeQueue.pop();
     let total = sumQueue.pop();
     // is leaf node
    if(!node.left && !node.right && targetSum === total) {
      return true;
    } else {
      if(node.left) {
        nodeQueue.push(node.left);
        sumQueue.push(node.left.val + total);
      }
      if(node.right) {
        nodeQueue.push(node.right);
        sumQueue.push(node.right.val + total);
      }
    }
  }
  return false;
};

var hasPathSum = function(root, targetSum) {
  if(!root) {
    // no exist root node
    return false;
  }
  // BFS 广度优先算法
  const nodeQueue = [root];
  const sumQueue = [root.val];
  // 队列不空
  while(nodeQueue.length) {
     let node = nodeQueue.shift();
     let total = sumQueue.shift();
     // let node = nodeQueue.pop();
     // let total = sumQueue.pop();
     // is leaf node
    if(!node.left && !node.right) {
      if(targetSum === total) {
         return true;
      } else {
        // const temp = total - node.value;
        // nodeQueue.pop();
        // sumQueue.pop();
        // sumQueue.push(temp);
        continue;
      }
    } else {
      if(node.left) {
        nodeQueue.push(node.left);
        // ❌  val !== value
        sumQueue.push(node.value + total);
      }
      if(node.right) {
        nodeQueue.push(node.right);
        // ❌  val !== value
        sumQueue.push(node.value + total); 
      }
    }
  }
  return false;
};


257. Binary Tree Paths

  1. 二叉树路径

https://leetcode.com/problems/binary-tree-paths/

https://leetcode-cn.com/problems/binary-tree-paths/


var binaryTreePaths = function (root) {
    const result = [];
    const traverseTree = (node, path) => {
        if(!node.left && !node.right) {
            path += node.val;
            // leaf node
            result.push(path);
            return;
        } else {
            path += node.val + '->';
        }
        if(node.left) {
            traverseTree(node.left, path);
        }
        if(node.right) {
            traverseTree(node.right, path);
        }
        // 不需要 返回值
        // return path;
    };
    // DFS 深度优先算法
    traverseTree(root, '');
    return result;
};


var binaryTreePaths = function (root) {
    const result = [];
    const traverseTree = (node, path) => {
        if(!node.left && !node.right) {
            path += node.val;
            // leaf node
            result.push(path);
        } else {
            path += node.val + '->';
        }
        if(node.left) {
            traverseTree(node.left, path);
        }
        if(node.right) {
            traverseTree(node.right, path);
        }
        return path;
    };
    // DFS 深度优先算法
    traverseTree(root, '');
    return result;
};

/**
 * 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)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function (root) {
    const result = [];
    const path = [];
    function findPath (node) {
        if(node == null) {
            return;
        };
        path.push(node.val);
        if(!node.left && !node.right) {
            result.push(path.join('->'));
        }
        findPath(node.left);
        findPath(node.right);
        // 删除已访问的 child node value
        path.pop();
    }
    // DFS
    findPath(root);
    return result;
};



refs

https://github.com/xgqfrms/learning/issues/120



©xgqfrms 2012-2020

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

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


posted @ 2022-02-07 23:16  xgqfrms  阅读(40)  评论(8编辑  收藏  举报