104.二叉树的最大深度
思路
● 递归或层序遍历,两种方法
代码
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
Deque<TreeNode> queue = new ArrayDeque();
int maxDepth = 0;
queue.addLast(root);
while (!queue.isEmpty()) {
int size = queue.size();
maxDepth++;
while (size-- > 0) {
TreeNode temp = queue.removeFirst();
if (temp.left != null) queue.addLast(temp.left);
if (temp.right != null) queue.addLast(temp.right);
}
}
return maxDepth;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
559.n叉树的最大深度
思路
● 类似,递归或迭代
代码
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int temp = 0;
for (Node child : root.children) {
temp = Math.max(temp, maxDepth(child));
}
return temp + 1;
}
}
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
Deque<Node> queue = new ArrayDeque();
int maxDepth = 0;
queue.addLast(root);
while (!queue.isEmpty()) {
int size = queue.size();
maxDepth++;
while (size-- > 0) {
Node temp = queue.removeFirst();
for (Node child : temp.children) {
if (child != null) queue.addLast(child);
}
}
}
return maxDepth;
}
}
111.二叉树的最小深度
思路
● 力扣题目链接
● 给定一个二叉树,找出其最小深度。
● 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
代码
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null) return minDepth(root.right) + 1;
if (root.right == null) return minDepth(root.left) + 1;
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Deque<TreeNode> queue = new ArrayDeque();
int minDepth = 0;
queue.addLast(root);
while (!queue.isEmpty()) {
int size = queue.size();
minDepth++;
while (size-- > 0) {
TreeNode temp = queue.removeFirst();
// 层序遍历,找到了叶子节点,那就找到了最小深度,直接返回
if (temp.left == null && temp.right == null) return minDepth;
if (temp.left != null) queue.addLast(temp.left);
if (temp.right != null) queue.addLast(temp.right);
}
}
return -1; // 程序其实根本不会走到这一步,前面一定可以找到叶子节点
}
}
222.完全二叉树的节点个数
● 力扣题目链接
● 给出一个完全二叉树,求出该树的节点个数。
思路
● 掌握普通二叉树的迭代和递归解法
● 完全二叉树的递归解法
代码
// 时间复杂度O(n) 空间复杂度O(logn)
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
// 时间复杂度O(n) 空间复杂度O(n)
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
Deque<TreeNode> queue = new ArrayDeque();
int count = 0;
queue.addLast(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
TreeNode temp = queue.removeFirst();
count++;
if (temp.left != null) queue.addLast(temp.left);
if (temp.right != null) queue.addLast(temp.right);
}
}
return count;
}
}
// 使用完全二叉树的性质,如果是满二叉树,节点个数是 2 ^ (height) - 1
// 如果不是满二叉树,不断递归左右孩子,遇到满二叉树再套公式计算
class Solution {
public int countNodes(TreeNode root) {
// 看是否是满二叉树,是就给结果,不是再继续递归
if (root == null) return 0;
TreeNode left = root.left;
TreeNode right = root.right;
int leftDepth = 0;
int rightDepth = 0;
while (left != null) {
leftDepth++;
left = left.left;
}
while (right != null) {
rightDepth++;
right = right.right;
}
// 是满二叉树
if (leftDepth == rightDepth) {
return (2 << (leftDepth)) - 1;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}