LeetCode104.二叉树的最大深度
题目描述:
给定一个二叉树 root
,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1:
输入:root = [3,9,20,null,null,15,7] 输出:3
示例 2:
输入:root = [1,null,2] 输出:2
解题思路:
·看到题目后有两个思路:一个是使用递归法,求最大深度,一个是使用层序遍历,求最大深度
·本题可以使用前序遍历,也可以使用后序遍历,使用前序遍历求的是深度,使用后序遍历求的是高度。所以我们使用后序遍历进行求解。
递归法
·现在,让我们再次复习一次递归的三件套:
1.确定递归函数的参数和返回值:参数就是传入树的根节点,返回值就是这棵树的深度1,所以返回值为int类型。代码如下:
int getdepth(TreeNode* node)
2.确定终止条件:如果为空则返回0,表示高度为0.代码如下:、
if(node == NULL) return 0;
3.确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后,取左右深度的最大数值再+1(+1是因为算上当前中间节点),即可求出当前二叉树的深度。
代码如下:
class solution {
public:
int getdepth(TreeNode* node) {
if (node == NULL) return 0;
int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;
}
int maxDepth(TreeNode* root) {
return getdepth(root);
}
};
易错点
*有同学分不清二叉树的深度和高度是什么意思
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数
二叉树的节点高度:指从该节点到叶子节点的最长简单路径的条数或者节点数
而根节点的高度就是二叉树的最大深度,所以我们通过后序求的根节点高度求的二叉树最大深度。
迭代法
二叉树求最大深度,与求二叉树层数刚好符合,所以,可以直接用求层序遍历进行求解
代码如下:
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
queue<TreeNode*> que;
int depth = 0;
que.push(root);
while(!que.empty()){
int size = que.size();
depth++;
for(int i = 0;i < size;i++){
TreeNode* node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return depth;
}
};
LeetCode111.二叉树的深度
题目描述:
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7] 输出:2
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6] 输出:5
解题思路:
·和上一题的解题思路基本一致,依旧可以使用递归法与迭代法解题
·需要搞明白最小深度,指的是根节点到叶子节点的最小深度
递归法:
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == NULL) return 0;
int leftDepth = getDepth(node->left); // 左
int rightDepth = getDepth(node->right); // 右
// 中
// 当一个左子树为空,右不为空,这时并不是最低点
if (node->left == NULL && node->right != NULL) {
return 1 + rightDepth;
}
// 当一个右子树为空,左不为空,这时并不是最低点
if (node->left != NULL && node->right == NULL) {
return 1 + leftDepth;
}
int result = 1 + min(leftDepth, rightDepth);
return result;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
易错点:
·第一点与前一题一样,要分清楚,前序求的是深度,后序求的是高度,所以这题采用的是后序遍历。
·求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑
迭代法
依旧是层次遍历,加上一个逻辑判断:只有当左右孩子都为空时,说明遍历到最低点
代码如下:
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size = que.size();
depth++;
for(int i = 0;i < size;i++){
TreeNode* node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
if(!node->left && ! node->right){
return depth;
}
}
}
return depth;
}
};
LeetCode222.完全二叉树的节点个数
题目描述:
给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2h
个节点。
示例 1:
输入:root = [1,2,3,4,5,6] 输出:6
示例 2:
输入:root = [] 输出:0
示例 3:
输入:root = [1] 输出:1
解题思路:
·使用递归法,使用哪种遍历方法都行,没经过一个结点,就加一,最后返回结果即可
·使用迭代法,也就是使用层序遍历,依旧统计结点数量
递归法
class Solution {
private:
int getNodesNum(TreeNode* cur) {
if (cur == NULL) return 0;
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
}
public:
int countNodes(TreeNode* root) {
return getNodesNum(root);
}
};
迭代法
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int result = 0;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
result++;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};
总结:今天的题目都是使用递归或迭代法进行求解,所以可见再二叉树甚至图论中,递归和迭代的重要性了