104. Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
- The number of nodes in the tree is in the range
[0, 104]. -100 <= Node.val <= 100
最大深度指的是根节点到最远叶子节点的路径上的节点个数。 最大高度指的是当前节点到最远叶子结点的路径上的节点个数。
/**
* 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 maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
int ldepth = maxDepth(root.left);
int rdepth = maxDepth(root.right);
return Math.max(ldepth+1, rdepth+1);
}
}
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Constraints:
- The number of nodes in the tree is in the range
[0, 105]. -1000 <= Node.val <= 1000
最小深度的题目有坑,在非叶子节点仅有一个子节点时,不能以空子节点的深度0作为返回值。 简单说就是非叶子节点的空子节点不计算深度。 由于递归每层+1,非叶子节点的空子节点深度设为Integer.MAX_VALUE - 1
/**
* 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;
}
if(root.left == null && root.right == null) {
return 1;
}
int ldepth = 0;
int rdepth = 0;
if(root.left!= null) {
ldepth = minDepth(root.left);
}
else {
ldepth = Integer.MAX_VALUE-1;
}
if(root.right!= null) {
rdepth = minDepth(root.right);
}
else {
rdepth = Integer.MAX_VALUE-1;
}
return Math.min(ldepth+1, rdepth+1);
}
}
222. Count Complete Tree Nodes
Given the root of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Design an algorithm that runs in less than O(n) time complexity.
Example 1:
Input: root = [1,2,3,4,5,6]
Output: 6
Example 2:
Input: root = []
Output: 0
Example 3:
Input: root = [1]
Output: 1
Constraints:
- The number of nodes in the tree is in the range
[0, 5 * 104]. 0 <= Node.val <= 5 * 104- The tree is guaranteed to be complete.
这题一开始想多了,因为题目要求实现小于O(N)的时间复杂度,事实上是可能的,如果先找到层数,在找到前序遍历的第一个左右子节点至少一个为空的节点,就能直接计算出总结点数。但问题是最后一个非满二叉树子节点,是需要知道它的同层位置,而要知道同层位置,BFS比前序更适合。可是如果采用BFS,还一样是O(N)的复杂度。 回头用递归O(N)复杂度实现,很好写。
/**
* 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 countNodes(TreeNode root) {
if(root == null) {
return 0;
}
int lcount = countNodes(root.left);
int rcount = countNodes(root.right);
return lcount + rcount + 1;
}
}