一种思路解决二叉树的普遍问题(JavaScript)面试必看

51 篇文章 1 订阅
47 篇文章 1 订阅
  • 二叉树相关算法总结

1、先序遍历
2、中序遍历
3、后序遍历
4、相同的树
5、翻转二叉树
6、二叉搜索树的第k小的元素
7、二叉树的层序遍历
8、二叉树的最大深度
9、N叉树的先序遍历
10、二叉树的所有路径
11、平衡二叉树( -这里是具体的题目思路描述
12、二叉树的直径


1、先序遍历


//二叉树遍历
//先序遍历
const preOrderTraversal = function(root){
  let res = []
  function traversal (root){
    if(root!==null){
      res.push(root.val)
      traversal(root.left)
      traversal(root.right)
    }
  } 
  traversal(root)
  return res
}

2、中序遍历

//中序遍历
const midOrderTraversal = function(root){
  let res = []
  function traversal (root){
    if(root!==null){
      traversal(root.left)
      res.push(root.val)
      traversal(root.right)
    }
  } 
  traversal(root)
  return res
}

3、后序遍历

//后序遍历
const postOrderTraversal = function(root){
  let res = []
  function traversal (root){
    if(root!==null){
      traversal(root.left)     
      traversal(root.right)
      res.push(root.val)
    }
  } 
  traversal(root)
  return res
}

4、相同的树

//相同的树
const sameTree = function(p,q){
  function traversal(p,q){
    if(p!==null&&q===null){
      return false
    }
    else if(p===null && q !== null){
      return false
    }
    else if(p===null && q===null){
      return true
    }
    else{
      return  p.val===q.val&&traversal(p.left,q.right)&& traversal(p.right,q.left)
    }
  }
    return  traversal(p,q)
}

5、翻转二叉树

//翻转二叉树
const reverse = function(root){
  function traversal(root){
    if(root=== null) return
    else{
      [root.left,root.right] = [traversal(root.right),traversal(root.left)]
      return root
    }
  }
  traversal(root)
}

6、二叉搜索树的第k小的元素

//二叉搜索树的第k小的元素
const kthSmallest = function(root,k){
  let res = []
  function traversal(root){
    if(!root&& res.length < k){
      traversal(root.left)
      res.push(root.val)
      traversal(root.right)
    }
  }
  traversal(root)
  return res[k-1]
}

7、二叉树的层序遍历

//二叉树的层序遍历
const OrderTraversal = function(root){
  let res = []
  function traversal(root,depth){
    if(!root){
      if(!res[depth]){
        res[depth] = []
      }
      else{
        traversal(root.left, depth + 1)
        res[depth].push(root.val)
        traversal(root.right, depth + 1)

      }
    }

  }
  traversal(root,0)
  return res
}

8、二叉树的最大深度

//二叉树的最大深度
var maxDepth = function (root) {
  let res = 0
  function traversal (root, depth) {
    if (root !== null) {
      if (depth > res) {
        res = depth
      }
      if (root.left) {
        traversal(root.left, depth + 1)
      }
      if (root.right) {
        traversal(root.right, depth + 1)
      }
    }
  }
  traversal(root, 1)
  return res
}

9、N叉树的先序遍历

//N叉树的先序遍历
const preNRoot  = function(root){
  let res = []
  function traversal(root){
    res.push(root.val)
    root.children.forEach(child=>traversal(child))
  }
  traversal(root)
  return root
}

10、二叉树的所有路径

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */

var binaryTreePaths = function(root) {
    const paths = [];
    const traversal = (root, path) => {
        if (root) {
            path += root.val;
            if (root.left === null && root.right === null) { // 当前节点是叶子节点
                paths.push(path); // 把路径加入到答案中
            } else {
                path += "->"; // 当前节点不是叶子节点,继续递归遍历
                traversal(root.left, path);
                traversal(root.right, path);
            }
        }
    }
    traversal(root, "");
    return paths;
};

11、平衡二叉树

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
    return height(root)>=0
};

var height = (root)=>{
    if(root == null ){
        return 0
    }
    let lr = height(root.left)
    let rr = height(root.right)
    console.log(lr,rr)
    if(lr>=0&&rr>=0&&Math.abs(lr-rr)<=1){
        return Math.max(lr,rr)+1
    }
    else{
        return -1
    }
}

12、二叉树的直径

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var diameterOfBinaryTree = function(root) {
    // 默认为1是因为默认了根节点自身的路径长度
    let ans = 1;

    function depth(rootNode) {
        if (!rootNode) {
            // 如果不存在根节点,则深度为0
            return 0;
        }
        // 递归,获取左子树的深度
        let L = depth(rootNode.left);
        // 递归,获取右子树的深度
        let R = depth(rootNode.right);

        /* 关键点1
        L+R+1的公式是如何而来?
        等同于:左子树深度(节点个数) + 右子树深度(节点个数) + 1个根节点
        便是这株二叉树从最左侧叶子节点到最右侧叶子节点的最长路径
        类似于平衡二叉树的最小值节点到最大值节点的最长路径
        之所以+1是因为需要经过根节点
         */
        // 获取该树的最长路径和现有最长路径中最大的那个
        ans = Math.max(ans, L + R + 1);
        /* 关键点2
        已知根节点的左右子树的深度,
        则,左右子树深度的最大值 + 1,
        便是以根节点为数的最大深度*/
        return Math.max(L, R) + 1;
    }

    depth(root);

    // 由于depth函数中已经默认加上数节点的自身根节点路径了,故此处需减1
    return ans - 1;
}; 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值