二叉树的中序遍历、深度【JS】

目录

  1. 二叉树的中序遍历
  • 1.1 递归法
  • 1.2 利用辅助栈的迭代法
  1. 二叉树的深度
  • 2.1 递归

  • 2.2 层序遍历树,深度为最大层数

    2.2.1 二叉树的层数遍历:leetcode题目1
    2.2.2 二叉树的层数遍历:leetcode题目2
    2.2.3 之字行打印二叉树

  1. 验证二叉搜索树
  • 3.1 递归
  • 3.2 迭代
1.二叉树的中序遍历

力扣带图官方解法链接

1.1 递归法(算法笔记上介绍二叉树的遍历只写了这种方法),实现比较简单。访问顺序为:左子节点,根节点,右子节点。
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
    // 简单的递归法
    if (!root) {
        return []
    }
    var res = []
    inorder(root, res)
    return res
};
var inorder = function (root, res) {
    if (!root) {
        return
    }
    inorder(root.left, res)
    res.push(root.val)
    inorder(root.right, res)
}
复杂度分析

时间复杂度:O(n)。
空间复杂度:最坏情况下需要空间O(n),平均情况为O(logn)。

1.2利用辅助栈的迭代法
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var inorderTraversal = function (root) {
    // 利用栈的迭代法
    if (!root) {
        return []
    }
    let res = []
    let stack = []
    let cur = root
    while (cur || stack.length) {
        while (cur) {
            stack.push(cur)
            cur = cur.left
        }
        cur = stack.pop()
        res.push(cur.val)
        cur = cur.right
    }
    return res
} 
复杂度分析

时间复杂度:O(n)。

空间复杂度:O(n)。

2.二叉树的深度
2.1递归

当前节点的深度为子结点的最大深度+1

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
    if (!root) return 0
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1
};
2.2 层序遍历树,深度为最大层数
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
    if (!root) return 0
    let arr = [root]
    let ans = 0
    while (arr.length) {
        let num = arr.length // 记录这一层节点的个数
        ans++
        while (num) { // 遍历当前层节点,把子结点入队
            let p = arr.shift()
            // nodes.push(p.val)
            if (p.left) arr.push(p.left)
            if (p.right) arr.push(p.right)
            num--
        }
    }
    return ans
};
2.2.1 二叉树的层数遍历:leetcode题目1
	3
   / \
  9  20
    /  \
   15   7

返回[3,9,20,15,7]

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var levelOrder = function (root) {
    if (!root) return []
    let ans = []
    let queue = [root]
    while (queue.length) {
        let p = queue.shift()
        ans.push(p.val)
        if (p.left) queue.push(p.left)
        if (p.right) queue.push(p.right)
    }
    return ans
};
2.2.2 二叉树的层数遍历:leetcode题目2

返回

[
  [3],
  [9,20],
  [15,7]
]
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function (root) {
    if (!root) return []
    let queue = [root]
    let ans = []
    while (queue.length) {
        let num = queue.length
        let tmp = []
        while (num--) {
            let p = queue.shift()
            tmp.push(p.val)
            if (p.left) queue.push(p.left)
            if (p.right) queue.push(p.right)
        }
        ans.push(tmp)
    }
    return ans
};
2.2.3 之字行打印二叉树

返回

[
  [3],
  [20,9],
  [15,7]
]

加一个标志,每隔一层逆转当前层的遍历结果数组

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function (root) {
    if (!root) return []
    let queue = [root]
    let ans = []
    let flag = true
    while (queue.length) {
        let num = queue.length
        let tem = []
        while (num--) {
            let p = queue.shift()
            tem.push(p.val)
            if (p.left) queue.push(p.left)
            if (p.right) queue.push(p.right)
        }
        if (!flag) tem.reverse()
        ans.push(tem)
        flag = !flag
    }
    return ans
};
3. 验证二叉搜索树
3.1 递归
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function (root) {
    if (!root) return true
    let cur = root
    let arr = []
    inoderBinary(root, arr)
    for (let i = 0; i < arr.length - 1; ++i) {
        if (arr[i] >= arr[i + 1]) return false
    }
    return true
};
var inoderBinary = function (root, arr) {
    if (!root) return
    if (root.left) inoderBinary(root.left, arr)
    arr.push(root.val)
    if (root.right) inoderBinary(root.right, arr)
}
3.2 迭代
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function (root) {
    if (!root) return true
    let cur = root
    let arr = []
    let stack = []
    while (cur || stack.length) {
        while (cur) {
            stack.push(cur)
            cur = cur.left
        }
        cur = stack.pop()
        arr.push(cur.val)
        cur = cur.right
    }
    for (let i = 0; i < arr.length - 1; ++i) {
        if (arr[i] >= arr[i + 1]) return false
    }
    return true
};

你可能感兴趣的:(二叉树的中序遍历、深度【JS】)