代码随想录算法训练营第16天 | 104.二叉树的最大深度 (优先掌握递归), 111.二叉树的最小深度 (优先掌握递归), 222.完全二叉树的节点个数(优先掌握递归)
104.二叉树的最大深度 (优先掌握递归)
- 求高度用后序遍历 因为是从下往上 要后处理 +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 maxDepth(TreeNode root) {
//后序遍历求根节点高度
return getHeight(root);
}
public int getHeight(TreeNode node){
//终止条件
if(node==null) return 0;
//后序遍历 左右中
int leftheight = getHeight(node.left);
int rightheight = getHeight(node.right);
int res = Math.max(leftheight,rightheight)+1;
return res;
}
}
559. N 叉树的最大深度
- 递归遍历一样是用后序遍历 只是没有左右 而是需要去递归 node的所有子节点 也就是遍历 node.children数组 去getdepth
- 然后再中的操作中 把depth+1
递归后序遍历
/*
// 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 {
public int maxDepth(Node root) {
return getdepth(root);
}
public int getdepth(Node node){
//终止条件
if(node==null) return 0;
int depth = 0;
//遍历子节点children数组
if(node.children!=null){
for(Node child:node.children){
depth = Math.max(depth,getdepth(child));
}
}
//中 的操作
return depth+1;
}
}
层序遍历求深度
- 层序遍历就是一层一层去添加进辅助队列 遍历每一层时都可以做 对应的操作 depth++
/*
// 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 {
public int maxDepth(Node root) {
Deque<Node> que = new LinkedList<>();
int depth = 0;
if(root!=null){
que.addLast(root);
}else{
return 0;
}
while(!que.isEmpty()){
int size = que.size();
while(size--!=0){
Node node = que.pollFirst();
for(Node child:node.children){
que.addLast(child);
}
}
depth++;
}
return depth;
}
}
111.二叉树的最小深度 (优先掌握递归)
- 注意点 题目中说 叶子节点是 没有子节点的节点 而左节点为空而右节点不为空 还有右节点为空左节点不为空 这种2情况不算在内
- 所以在计算高度的时候 不能简单取 左右子树 最小的高度 +1 而是要先判断 是否为以上两种情况
- 若为以上2种情况 就需要 return 不为空的子树的高度+1
- 然后再 return min(leftheight,rightheight)+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) {
return getHeight(root);
}
public int getHeight(TreeNode node){
//终止情况
if(node==null) return 0;
//后序遍历
int leftheight = getHeight(node.left);
int rightheight = getHeight(node.right);
//中 操作 判断是否有一个子节点为空的情况
if(node.left==null && node.right!=null){
return rightheight+1;
}
if(node.left!=null && node.right==null){
return leftheight+1;
}
int res = Math.min(leftheight,rightheight)+1;
return res;
}
}
222.完全二叉树的节点个数(优先掌握递归)
- 普通二叉树 就是递归遍历 每次+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 countNodes(TreeNode root) {
return getNum(root);
}
//后序遍历
public int getNum(TreeNode node){
//终止条件
if(node==null) return 0;
int leftnum = getNum(node.left);
int rightnum = getNum(node.right);
int res = leftnum+rightnum+1;
return res;
}
}
用满二叉树计算节点的公式 2^h-1 计算完全二叉树的节点数量
- 因为完全二叉树 是不存在中间节点为空而两边节点存在的情况
- 所以可以 计算左边和右边的深度是否相等来 判断该子树是否为 满二叉树
- 公式为 2^h-1 h要注意为深度 一开始从0开始计算 所以后面计算次方的时候要+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 countNodes(TreeNode root) {
return getNum(root);
}
//利用满二叉树计算公式
public int getNum(TreeNode node){
//终止条件 1 当节点为null
if(node==null) return 0;
TreeNode left = node.left;
TreeNode right = node.right;
int leftdepth = 0;
int rightdepth = 0;
while(left!=null){
left = left.left;
leftdepth++;
}
while(right!=null){
right = right.right;
rightdepth++;
}
//终止条件2 当左右的深度相等 可以构成满二叉树
if(leftdepth==rightdepth){
return (int)Math.pow(2,leftdepth+1)-1; //leftdepth深度一般一开始是从0算的 这样的话算就要+1
}
//左右中
int leftheight = getNum(node.left);
int rightheight = getNum(node.right);
int res = leftheight + rightheight +1;
return res;
}
}