day16 打卡104.二叉树的最大深度 559. N 叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数
day16 打卡104.二叉树的最大深度 559. N 叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数
104.二叉树的最大深度
1.递归法
class Solution {
public int maxDepth(TreeNode root) {
return maxDepth(root, 0);
}
public int maxDepth(TreeNode cur, int depth) {
if (cur == null) return depth;
depth++;
int left = maxDepth(cur.left, depth);
int right = maxDepth(cur.right, depth);
return left > right ? left : right;
}
}
2.迭代法,即层次遍历
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int size = 1;
int depth = 0;
while (!que.isEmpty()) {
size = que.size();
depth++;
while (size>0) {
TreeNode cur = que.poll();
if (cur.left != null) que.offer(cur.left);
if (cur.right != null) que.offer(cur.right);
size--;
}
}
return depth;
}
}
559. N 叉树的最大深度
递归法,与104题目一样的思路
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int depth = 0;
if (root.children != null) {
for (Node node: root.children) {
depth = Math.max(depth, maxDepth(node));
}
}
return depth + 1;
}
}
111.二叉树的最小深度
1.递归法
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if (root.left == null && root.right != null) {
return rightDepth + 1;
}
if (root.left != null && root.right == null) {
return leftDepth + 1;
}
return Math.min(leftDepth, rightDepth) + 1;
}
}
2.迭代法,即层次遍历
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int size = 1;
int depth = 0;
while (!que.isEmpty()) {
size = que.size();
depth++;
while (size>0) {
TreeNode cur = que.poll();
if (cur.left != null) que.offer(cur.left);
if (cur.right != null) que.offer(cur.right);
if (cur.left == null && cur.right == null) return depth;
size--;
}
}
return depth;
}
}
222.完全二叉树的节点个数
1.递归法
class Solution {
public int countNodes(TreeNode node) {
if (node == null) return 0;
int leftCount = countNodes(node.left);
int rightCount = countNodes(node.right);
return leftCount + rightCount + 1;
}
}
2.迭代法,即层次遍历
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int size = 1;
int count = 0;
while (!que.isEmpty()) {
size = que.size();
while (size>0) {
TreeNode cur = que.poll();
count++;
if (cur.left != null) que.offer(cur.left);
if (cur.right != null) que.offer(cur.right);
size--;
}
}
return count;
}
}
3.针对完全二叉树的解法,满二叉树的结点数为:2^depth - 1
class Solution {
public int countNodes(TreeNode node) {
if (node == null) return 0;
TreeNode left = node.left;
TreeNode right = node.right;
int leftCount = 0;
int rightCount = 0;
while (left != null) {
left = left.left;
leftCount++;
}
while (right != null) {
right = right.right;
rightCount++;
}
if (leftCount == rightCount) {
// 说明是满二叉树
return ( 2 << leftCount) - 1;
}
return countNodes(node.left) + countNodes(node.right) + 1;
}
}

浙公网安备 33010602011771号