104. 二叉树的最大深度
[题目链接] (leetcode.cn/problems/ma…)
要求: 给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
思路
1.递归法
后序遍历
var maxDepth = function(root) {
//后序遍历求高度,高度是任意节点到叶子节点的距离,根节点的高度就是二叉树的最大深度
let depth = 0
function getDepth(node){
if(node == null) return 0
let leftDepth = getDepth(node.left)
let rightDepth = getDepth(node.right)
return 1 + Math.max(leftDepth, rightDepth)
}
depth = getDepth(root)
return depth
};
2.前序遍历
var maxDepth = function(root) {
//前序节点求深度,深度是任意节点到根节点的距离
function getDepth(node, depth){
//中左右
res = res >depth ? res : depth
if(node.left == null && node.right == null) return
if(node.left) {
depth++
getDepth(node.left, depth)
depth--
}
if(node.right){
depth++
getDepth(node.right, depth)
depth--
}
return
}
let res = 0
if(root == null) return res
getDepth(root, 1)
return res
};
3.层序遍历
var maxDepth = function(root) {
//层序遍历
let queue = [root]
let res = 0
if(root == null) return res
while(queue.length){
let size = queue.length
res++
for(let i=0; i<size; i++){
let node = queue.shift()
if(node.left)queue.push(node.left)
if(node.right)queue.push(node.right)
}
}
return res
};
111. 二叉树的最小深度
要求:给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
思路
如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
1.递归法
后序遍历
var minDepth = function(root) {
//后序遍历
function getDepth(node){
if(node == null) return 0
let leftDepth = getDepth(node.left)
let rightDepth = getDepth(node.right)
if(node.left == null && node.right != null){
return 1+rightDepth
}
if(node.left != null && node.right == null){
return 1+leftDepth
}
return 1+Math.min(leftDepth, rightDepth)
}
if(root == null) return 0
return getDepth(root)
};
- 前序遍历
var minDepth = function(root) {
//前序遍历
let res = Infinity
if(root == null) return 0
getDepth(root, 1)
return res
function getDepth(node, depth){
if(node == null) return
if(node.left == null && node.right == null){
res = Math.min(res, depth)
}
if(node.left){
depth++
getDepth(node.left, depth)
depth--
}
if(node.right){
depth++
getDepth(node.right, depth)
depth--
}
}
};
3.层序遍历
var minDepth = function(root) {
//层序遍历
let queue = [root]
let res = 0
if(root == null) return 0
while(queue.length){
let size = queue.length
res++
while(size--){
let node = queue.shift()
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
//如果节点的左右节点都为空,则返回深度
if(node.left == null && node.right == null) return res
}
}
};
222. 完全二叉树的节点个数
要求:给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
思路
普通二叉树
1.层序遍历
var countNodes = function(root) {
let queue = [root]
let res = 0
if(root == null) return 0
while(queue.length){
let size = queue.length
while(size--){
let node = queue.shift()
res++
if(node.left)queue.push(node.left)
if(node.right)queue.push(node.right)
}
}
return res
};
2.递归法
var countNodes = function(root) {
if(root == null) return 0
return getNodes(root)
function getNodes(node){
if(node == null) return 0
let leftNode = getNodes(node.left)
let rightNode = getNodes(node.right)
return 1+leftNode+rightNode
}
};
完全二叉树
var countNodes = function(root) {
if(root == null) return 0
let left = root.left, right = root.right
console.log(left, right)
let leftDepth =1, rightDepth = 1
while(left){
left = left.left
leftDepth++
}
while(right){
right = right.right
rightDepth++
}
console.log(leftDepth, rightDepth)
if(leftDepth == rightDepth){
return 2**leftDepth-1
}
let countLeft = countNodes(root.left)
let countRight = countNodes(root.right)
return 1+countLeft+countRight
};