题目:104. 二叉树的最大深度 - 力扣(LeetCode)
思路/想法:
递归法:力扣底层应该给处理了。
代码实现:
// 递归法:力扣底层应该给处理了
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}
// 递归法
class Solution {
int maxValue = 0;
public int maxDepth(TreeNode root) {
level(root, 0);
return maxValue;
}
private void level(TreeNode node, int temp) {
if (node == null) {
return;
}
temp++;
maxValue = Math.max(maxValue, temp);
level(node.left, temp);
level(node.right, temp);
temp--;
}
}
// 迭代法:
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int depth = 0;
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
while (!que.isEmpty()) {
depth++;
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode node = que.poll();
if (node.left != null) que.add(node.left);
if (node.right != null) que.add(node.right);
}
}
return depth;
}
}
题目:559. N 叉树的最大深度 - 力扣(LeetCode)
思路/想法:
代码实现:
// 递归法
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int depth = 0;
if (root.children != null) {
for (Node child : root.children) {
depth = Math.max(depth, maxDepth(child));
}
}
return depth + 1;
}
}
// 迭代法
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int depth = 0;
Queue<Node> que = new LinkedList<>();
que.add(root);
while (!que.isEmpty()) {
int size = que.size();
depth++;
while (size > 0) {
Node node = que.poll();
if (node != null) {
for (int i = 0; i < node.children.size(); i++) {
que.add(node.children.get(i));
}
}
size--;
}
}
return depth;
}
}
题目:111. 二叉树的最小深度 - 力扣(LeetCode)
思路/想法:
迭代模板大法
代码实现:
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
int depth = 0;
while (!que.isEmpty()) {
int size = que.size();
depth++;
for (int i = 0; i < size; i++) {
TreeNode node = que.poll();
if (node.right == null && node.left == null) {
return depth;
}
if (node.left != null) que.add(node.left);
if (node.right != null) que.add(node.right);
}
}
return depth;
}
}
// 递归法
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) return rightDepth + 1;
if (root.right == null) return leftDepth + 1;
return Math.min(leftDepth, rightDepth) + 1;
}
}
题目:222. 完全二叉树的节点个数 - 力扣(LeetCode)
思路/想法:
模板大法,很酷
代码实现:
// 递归法
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
int leftCount = countNodes(root.left);
int rightCount = countNodes(root.right);
return leftCount + rightCount + 1;
}
}
// 迭代法
class Solution {
public int countNodes(TreeNode root) {
int count = 0;
if (root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
while (!que.isEmpty()) {
int size = que.size();
count += size;
while (size > 0) {
TreeNode node = que.poll();
if (node.left != null) que.add(node.left);
if (node.right != null) que.add(node.right);
size--;
}
}
return count;
}
}