目录
2.1 递归
2.2 层序遍历树,深度为最大层数
2.2.1 二叉树的层数遍历:leetcode题目1
2.2.2 二叉树的层数遍历:leetcode题目2
2.2.3 之字行打印二叉树
力扣带图官方解法链接
/**
* 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)。
/**
* 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)。
当前节点的深度为子结点的最大深度+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
};
/**
* 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
};
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
};
返回
[
[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
};
返回
[
[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
};
/**
* 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)
}
/**
* 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
};