第六章 二叉树part03
大纲
● 104.二叉树的最大深度 559.n叉树的最大深度
● 111.二叉树的最小深度
● 222.完全二叉树的节点个数
leetcode 104
二叉树的最大深度
思路
- 递归法除空节点外直接返回最大深度+1
- 迭代法借助队列逐层遍历 记录遍历的深度
细节
代码
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
else return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int depth = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
depth++;
}
return depth;
}
}
复杂度
- 时间
O(n)
- 空间
O(n)
leetcode 559
N 叉树的最大深度
思路
- 思路同求二叉树的最大深度
细节
代码
class Solution {
public int maxDepth(Node root) {
int depth = 0;
if (root == null) return depth;
for (Node child : root.children) depth = Math.max(depth, maxDepth(child));
return 1 + depth;
}
}
class Solution {
public int maxDepth(Node root) {
int depth = 0;
if (root == null) return depth;
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0)
for (Node child : queue.poll().children)
if (child != null) queue.offer(child);
depth++;
}
return depth;
}
}
复杂度
- 时间
O(n)
- 空间
O(n)
leetcode 111
二叉树的最小深度
思路
- 递归法有条件的递归返回
- 迭代法使用队列记录最小深度
细节
- 注意最小深度的判定条件应为左右子结点均为空出现的结点
代码
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left != null && root.right == null) return 1 + minDepth(root.left);
if (root.left == null && root.right != null) return 1 + minDepth(root.right);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}
class Solution {
public int minDepth(TreeNode root) {
int depth = 0;
if (root == null) return depth;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
depth++;
while (size-- > 0) {
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
if (node.left == null && node.right == null) return depth;
}
}
return depth;
}
}
复杂度
- 时间
O(n)
- 空间
O(n)
leetcode 222
思路
- 迭代法可利用慢二叉树性质进行迭代
细节
代码
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
}
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
int ld = getDepth(root.left), rd = getDepth(root.right);
if (ld == rd) return countNodes(root.right) + (1 << ld);
else return countNodes(root.left) + (1 << rd);
}
public int getDepth(TreeNode node) {
if (node == null) return 0;
return 1 + getDepth(node.left);
}
}
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
int count = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
count += size;
while (size-- > 0) {
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return count;
}
}
复杂度
- 时间
O(n)
- 空间
O(n)