Leetcode 104. 二叉树的最大深度
题目链接:104. 二叉树的最大深度
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。
代码实现(递归法)
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return 1 + Math.max(leftHeight, rightHeight);
}
}
当然层序遍历也可以,而且更简单。在层序遍历那篇博客中,也记录了这道题。
代码实现(迭代法)
class Solution {
public int maxDepth(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
if(root != null){
deque.add(root);
}
int depth = 0;
while(!deque.isEmpty()){
int size = deque.size();
depth++;
for(int i = 1; i <= size; i++){
TreeNode node = deque.poll();
if(node.left != null){
deque.add(node.left);
}
if(node.right != null){
deque.add(node.right);
}
}
}
return depth;
}
}
Leetcode 559. N 叉树的最大深度
题目链接:559. N 叉树的最大深度
代码实现(递归法)
class Solution {
public int maxDepth(Node root) {
if(root == null){
return 0;
}
int depth = 0;
for(int i = 0; i < root.children.size(); i++){
depth = Math.max(depth, maxDepth(root.children.get(i)));
}
return depth + 1;
}
}
代码实现(迭代法)
class Solution {
public int maxDepth(Node root) {
Deque<Node> deque = new LinkedList<>();
if(root != null){
deque.add(root);
}
int depth = 0;
while(!deque.isEmpty()){
int size = deque.size();
depth++;
for(int i = 1; i <= size; i++){
Node node = deque.poll();
List<Node> children = node.children;
if(children == null || children.size()== 0){
continue;
}
for(Node child : children){
if(child != null){
deque.add(child);
}
}
}
}
return depth;
}
}
Leetcode 111. 二叉树的最小深度
题目链接:111. 二叉树的最小深度
和上一题的求最大深度看似差不多,其实差距还挺大,有坑。
本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。
代码实现(递归法)
class Solution {
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
int leftHeight = minDepth(root.left);
int rightHeight = minDepth(root.right);
if(root.left == null && root.right != null){
return rightHeight + 1;
}
if(root.left != null && root.right == null){
return leftHeight + 1;
}
return Math.min(leftHeight, rightHeight) + 1;
}
}
代码实现(迭代法)
class Solution {
public int minDepth(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
if(root != null){
deque.add(root);
}
int depth = 0;
while(!deque.isEmpty()){
int size = deque.size();
depth++;
for(int i = 1; i <= size; i++){
TreeNode node = deque.poll();
if(node.left == null && node.right == null){
return depth;
}
if(node.left != null){
deque.add(node.left);
}
if(node.right != null){
deque.add(node.right);
}
}
}
return depth;
}
}
Leetcode 222. 完全二叉树的节点个数
题目链接:222. 完全二叉树的节点个数
本题要利用完全二叉树的性质。
完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。
对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。
对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
代码实现
class Solution {
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
int leftDepth = 1;
int rightDepth = 1;
TreeNode left = root.left;
TreeNode right = root.right;
while(left != null){
left = left.left;
leftDepth++;
}
while(right != null){
right = right.right;
rightDepth++;
}
if(leftDepth == rightDepth){
return (1 << leftDepth) - 1;
}
int leftNodes = countNodes(root.left);
int rightNodes = countNodes(root.right);
return leftNodes + rightNodes + 1;
}
}