题目地址(559. N 叉树的最大深度)
https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
题目描述
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
示例 1:
输入:root = [1,null,3,2,4,null,5,6]
输出:3
示例 2:
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5
提示:
树的深度不会超过 1000 。
树的节点数目位于 [0, 104] 之间。
迭代法
代码
- 语言支持:C++
C++ Code:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if(!root)
return 0;
queue<Node*> que;
que.push(root);
int depth = 0;
while(!que.empty()){
int size = que.size();
depth++; // 深度++
for(int i=0; i<size; i++){
Node* node = que.front();
que.pop();
for(int j=0; j<node->children.size(); j++)
que.push(node->children[j]);
}
}
return depth;
}
};
复杂度分析
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
递归法
class Solution {
public:
int maxDepth(Node* root) {
if(!root)
return 0;
int depth = 0;
for(int i=0; i<root->children.size(); i++){
depth = max(depth, maxDepth(root->children[i]));
}
return depth + 1;
}
};
还有一道是求二叉树的最大深度问题,做做练练手
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
这里用前序遍历来实现
class Solution {
public:
int result;
void getMaxDepth(TreeNode* root, int depth){
result = max(result, depth); // 中
if(root->left){
depth++;
getMaxDepth(root->left, depth);
depth--;
}
if(root->right){ // 左
depth++; // 深度+1
getMaxDepth(root->right, depth);
depth--; // 回溯,深度-1
}
return;
}
int maxDepth(TreeNode* root) {
if(!root)
return 0;
getMaxDepth(root, 1);
return result;
}
};
题目地址(111. 二叉树的最小深度)
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
题目描述
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
提示:
树中节点数的范围在 [0, 105] 内
-1000 <= Node.val <= 1000
递归法
代码
- 语言支持:C++
C++ Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)
return 0;
int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);
// 左子树为空,右不为空
if(root->left == NULL && root->right)
return rightDepth + 1;
// 右子树为空,左不为空
if(root->left && root->right == NULL)
return leftDepth + 1;
return min(leftDepth, rightDepth) + 1;
}
};
迭代法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)
return 0;
queue<TreeNode*> que;
que.push(root);
int depth = 0;
while(!que.empty()){
int size = que.size();
depth++;
bool flag = false;
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 == NULL && node->right == NULL){
flag = true;
break;
}
}
if(flag == 1) break;
}
return depth;
}
};