Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD
目录
目录
104-二叉树的最大深度 Maximum Depth of Binary Tree
问题
深度优先(递归法)
广度优先(队列)方式一
广度优先(队列)方式二
111-二叉树的最小深度 Minimum Depth of Binary Tree
题目
递归
104-二叉树的最大深度 Maximum Depth of Binary Tree
问题
深度优先(递归法)
广度优先(队列)方式一
广度优先(队列)方式二
111-二叉树的最小深度 Minimum Depth of Binary Tree
题目
递归
104-二叉树的最大深度 Maximum Depth of Binary Tree
树 深度优先搜索(递归) 广度优先搜索(队列)
问题
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
方法声明:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public int maxDepth(TreeNode root) {
}
}
深度优先(递归法)
class Solution {
public int maxDepth(TreeNode root) {
int depth = 0;
if (root != null) {
depth = Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
return depth;
}
}
时间复杂度:我们每个结点只访问一次,因此时间复杂度为 O(N)
, 其中 N 是结点的数量。
空间复杂度:在最糟糕的情况下,树是完全不平衡的,例如每个结点只剩下左子结点,递归将会被调用 N 次(树的高度),因此保持调用栈的存储将是 O(N)
。但在最好的情况下(树是完全平衡的),树的高度将是 log(N)。因此,在这种情况下的空间复杂度将是 O(log(N))
。
广度优先(队列)方式一
广度优先搜索算法:
public static void levelTraversal(Node root) {
Queue<Node> queue = new LinkedList<>(); //LinkedList是Java中最普通的一个队列
queue.offer(root); //add、addLast
while (!queue.isEmpty()) {
Node node = queue.poll();//removeFirst
if (node != null) {
System.out.print(node.value);
queue.offer(node.left);
queue.offer(node.right);
}
}
}
修改后的算法:
class Solution {
public int maxDepth(TreeNode root) {
Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>();
queue.offer(new Pair<>(root, 0));
int depth = 0;
while (!queue.isEmpty()) {
Pair<TreeNode, Integer> current = queue.poll();
depth = Math.max(depth, current.depth); //更新 depth
if (current.node != null) {
queue.offer(new Pair<>(current.node.left, current.depth + 1));
queue.offer(new Pair<>(current.node.right, current.depth + 1));
}
}
return depth;
}
}
定义的辅助类
class Pair<TreeNode, Integer> {
TreeNode node;
Integer depth;
Pair(TreeNode node, Integer depth) {
this.node = node;
this.depth = depth;
}
}
时间复杂度:O(N)
空间复杂度:O(N)
广度优先(队列)方式二
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int depth = 0;
while (!queue.isEmpty()) {
depth++;
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return depth;
}
}
111-二叉树的最小深度 Minimum Depth of Binary Tree
树 深度优先搜索 递归
题目
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最小深度 2.
递归
class Solution {
public int minDepth(TreeNode root) {
if (root != null) {
if (root.left == null && root.right == null) return 1; //这个判断可以省略
else if (root.left == null) return minDepth(root.right) + 1;
else if (root.right == null) return minDepth(root.left) + 1;
else return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
} else return 0;
}
}
2018-12-8