day16 | 104.二叉树的最大深度、111.二叉树的最小深度、 222.完全二叉树的节点个数
目录:
链接
题目链接:
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/
https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
解题及思路学习
104. 二叉树的最大深度、559. N 叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7]
,
3/ \9 20/ \15 7
返回它的最大深度 3 。
思考:之前用层序遍历法得出的最大深度,现在尝试用递归方法求解。
递归三部曲:
- 确定递归函数的参数和返回值
2 确定终止条件
- 确定单层递归的逻辑
想法是:1、当节点为空的时候,直接返回0。2、传入节点指针,返回该节点的深度。3、每一个节点的深度等于最大的子节点深度+1。所以,直接求左右子树的深度,返回大的+1。
//我靠,我居然写出来了class Solution {
public:int maxDepth(TreeNode* root) {if (root == NULL) return 0;int count_left = 0;int count_right = 0;if(root->left) {count_left = maxDepth(root->left);}if(root->right) {count_right = maxDepth(root->right);}return count_left > count_right ? (count_left + 1) : (count_right + 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;return 1 + max(maxDepth(root->left), maxDepth(root->right));}
};
扩展题目: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 = max(depth, maxDepth(root->children[i]));}return depth + 1;}
};
root->children.size() 可以知道子树的总数。子树是以vector的形式存储的,所有可以通过下标访问每一棵子树。
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
**说明:**叶子节点是指没有子节点的节点。
示例 1:
!https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg
输入:root = [3,9,20,null,null,15,7]
输出:2
思考,这道题之前用层次遍历也做出来了,现在试试递归方法。
随想录:用后续遍历的方式求最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。注意是叶子节点。
什么是叶子节点,左右孩子都为空的节点才是叶子节点! 所以,在处理返回值的时候要分别判断左右子树是否为空。
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);}
};
迭代法思路很重要,要按照三个步骤来规划。并且处理单层逻辑的时候,要考虑多种状况。
222. 完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2h
个节点。
示例 1:
!https://assets.leetcode.com/uploads/2021/01/14/complete.jpg
输入:root = [1,2,3,4,5,6]
输出:6
思考:可以用层次遍历,没遍历一个节点,计数加1;
层次遍历每个节点代码:
class Solution {
public:int countNodes(TreeNode* root) {queue<TreeNode*> que;int result = 0;if (root != NULL) que.push(root);while(!que.empty()){int size = que.size();result += size;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 result;}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
尝试用递归的方法写一下:
(好像递归方法掌握那三个点之后写起来就变得很容易了)
class Solution {
public:int countNodes(TreeNode* root) {if (root == NULL) return 0;int leftCount = countNodes(root->left);int rightCount = countNodes(root->right);int result = leftCount + rightCount +1;return result;}
};
- 时间复杂度:O(n)
- 空间复杂度:O(log n),算上了递归系统栈占用的空间
随想录:利用满二叉树的特性(节点数= 2的n次方 -1),先判断左右子树是否是满二叉树,如果是,则直接计算节点。和普通二叉树不同之处,在于当遇到满二叉树的时候也会终止。
class Solution {
public:int countNodes(TreeNode* root) {if (root == nullptr) return 0;TreeNode* left = root->left;TreeNode* right = root->right;int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便while (left) { // 求左子树深度left = left->left;leftDepth++;}while (right) { // 求右子树深度right = right->right;rightDepth++;}if (leftDepth == rightDepth) {return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0}return countNodes(root->left) + countNodes(root->right) + 1;}
};
- 时间复杂度:O(log n × log n)
- 空间复杂度:O(log n)
困难及收获
困难
整体来说,今天的题目不难。
今日收获
对递归的理解更深了,一定要把握三个关键点。