104. Maximum Depth of Binary Tree
题目链接:104.二叉树的最大深度
思路链接:代码随想录二叉树-二叉树的最大深度
思路
思路
心路历程
心路历程
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 递归法
// 利用后续遍历
// 需要处理的参数:当前TreeNode
// 返回值:depth int
public int maxDepthHelper(TreeNode curr) {
// 结束条件,遍历到空节点时, depth为0
if (curr == null) {
return 0;
}
// 左
int leftDepth = maxDepthHelper(curr.left);
// 右
int rightDepth = maxDepthHelper(curr.right);
// 中
return 1 + Math.max(leftDepth, rightDepth);
}
public int maxDepth(TreeNode root) {
return maxDepthHelper(root);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
import java.util.Queue;
import java.util.LinkedList;
class Solution {
public int maxDepth(TreeNode root) {
// 迭代法
// 利用层序遍历
if (root == null) {
return 0;
}
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int depth = 0;
while (!que.isEmpty()) {
int size = que.size();
while (size > 0) {
TreeNode node = que.poll();
// System.out.println(node.val);
if (node.left != null) {
que.add(node.left);
}
if (node.right != null) {
que.add(node.right);
}
size--;
}
depth++;
}
return depth;
}
}
559. Maximum Depth of N-ary Tree
题目链接:559.n叉树的最大深度
思路链接:代码随想录二叉树-n叉树的最大深度
思路
思路
心路历程
心路历程
Code
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
// 利用后续遍历
// 需要处理的参数:当前Node
// 返回值:depth int
public int maxDepthHelper(Node curr) {
// 结束条件:当遍历节点为空时,返回depth为0
if (curr == null) {
return 0;
}
int maxNum = 0;
// 每个子节点
for (Node node : curr.children) {
int nodeDepth = maxDepthHelper(node);
maxNum = nodeDepth > maxNum ? nodeDepth : maxNum;
}
return 1 + maxNum;
}
public int maxDepth(Node root) {
return maxDepthHelper(root);
}
}
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
import java.util.Queue;
import java.util.LinkedList;
class Solution {
public int maxDepth(Node root) {
if (root == null) {
return 0;
}
Queue<Node> que = new LinkedList<>();
que.offer(root);
int depth = 0;
while (!que.isEmpty()) {
int size = que.size();
while (size > 0) {
Node node = que.poll();
for (Node child : node.children) {
if (child != null) {
que.offer(child);
}
}
size--;
}
depth++;
}
return depth;
}
}
111. Minimum Depth of Binary Tree
题目链接:111.二叉树的最小深度
思路链接:代码随想录二叉树-二叉树的最小深度
思路
思路
心路历程
心路历程
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 递归法 后续遍历
// 处理参数:当前节点;返回值:最小depth
public int minDepthHelper(TreeNode curr) {
if (curr == null) {
return 0;
}
// 左
int leftDepth = minDepthHelper(curr.left);
// 右
int rightDepth = minDepthHelper(curr.right);
// 中
if (curr.left == null) {
return 1 + rightDepth;
}
if (curr.right == null) {
return 1 + leftDepth;
}
return 1 + Math.min(leftDepth, rightDepth);
}
public int minDepth(TreeNode root) {
return minDepthHelper(root);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/**
* 迭代法,层序遍历
*/
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
int depth = 0;
while (!deque.isEmpty()) {
int size = deque.size();
depth++;
for (int i = 0; i < size; i++) {
TreeNode poll = deque.poll();
if (poll.left == null && poll.right == null) {
// 是叶子结点,直接返回depth,因为从上往下遍历,所以该值就是最小值
return depth;
}
if (poll.left != null) {
deque.offer(poll.left);
}
if (poll.right != null) {
deque.offer(poll.right);
}
}
}
return depth;
}
}
222. Count Complete Tree Nodes
题目链接:222.完全二叉树的节点个数
思路链接:代码随想录二叉树-完全二叉树的节点个数
思路
思路
心路历程
心路历程
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 递归法
public int countNodesHelper(TreeNode curr) {
// 结束条件
// 条件1
if (curr == null) {return 0;}
// 条件2
TreeNode left = curr.left;
TreeNode right = curr.right;
int leftDepth = 0;
int rightDepth = 0;
while (left != null) {
left = left.left;
leftDepth++;
}
while (right != null) {
right = right.right;
rightDepth++;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1;
}
// 递归操作
int leftTreeDepth = countNodesHelper(curr.left);
int rightTreeDepth = countNodesHelper(curr.right);
return leftTreeDepth + rightTreeDepth + 1;
}
public int countNodes(TreeNode root) {
return countNodesHelper(root);
}
}