第六章二叉树part03
104. 二叉树的最大深度
给定一个二叉树 root
,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
迭代法:
int maxDepth(TreeNode* root) {
//迭代法
queue<TreeNode*> que;
if(root!=nullptr) que.push(root);
int length=0;
while(!que.empty()){
int size=que.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);
}
length++;
}
return length;
}
递归法:
int maxDepth(TreeNode* root) {
//递归法
// int max_Depth=0;
// if(root==nullptr) return 0;
// if(root->left==nullptr&&root->right==nullptr) return 1;
// if(root->left!=nullptr){
// max_Depth=max(maxDepth(root->left),max_Depth);
// }
// if(root->right!=nullptr){
// max_Depth=max(maxDepth(root->right),max_Depth);
// }
// return max_Depth+1;
if(root==NULL) return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
}
559. N 叉树的最大深度
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
示例 1:
输入:root = [1,null,3,2,4,null,5,6] 输出:3
class Solution {
public:
int maxDepth(Node* root) {
int max_Depth=0;
if(root==NULL) return 0;
for(auto i:root->children){
max_Depth=max(max_Depth,maxDepth(i));
}
return max_Depth+1;
}
};
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
迭代法:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root!=nullptr) que.push(root);
int depth=0;
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;
}
递归法1
int minDepth(TreeNode* root) {
if(root==nullptr) return 0;
if(root->left==nullptr&&root->right==nullptr) return 1;
int min_depth=INT_MAX;
if(root->left!=nullptr){
min_depth=min(minDepth(root->left),min_depth);
}
if(root->right!=nullptr){
min_depth=min(minDepth(root->right),min_depth);
}
return min_depth+1;
}
递归法2(注意叶子节点指的是左右节点均为空的节点)
注意与求最大深度不同之处
class Solution {
public:
int getDepth(TreeNode* node){
if(node==NULL) return 0;
int leftDepth=getDepth(node->left);
int rightDepth=getDepth(node->right);
//此处值得注意,若不加一下判断则会将一些只有一个子节点的点误认为子节点并记深度为1
if(node->left==NULL&&node->right!=NULL){
return rightDepth+1;
}
if(node->right==NULL&&node->left!=NULL){
return leftDepth+1;
}
int result=1+min(leftDepth,rightDepth);
return result;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
222. 完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2^h
个节点。
层次遍历法(迭代法):
int countNodes(TreeNode* root) {
//迭代(层次遍历)
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
int sizes=0;
while(!que.empty()){
int size=que.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);
}
sizes+=size;
}
return sizes;
}
递归法
自己写的版本前序遍历中输出改成计数
class Solution {
public:
int count=0;
void traversal(TreeNode* root){
if(root==NULL) return;
count++;
if(root->left) traversal(root->left);
if(root->right) traversal(root->right);
}
int countNodes(TreeNode* root) {
traversal(root);
return count;
}
};
另一种递归(感觉是真正意义上递归求节点数)
class Solution {
public:
int getNodes(TreeNode* cur){
if(cur==NULL) return 0;
int leftSum=getNodes(cur->left); //获得左子树节点数目
int rightSum=getNodes(cur->right); //获得右子树节点书目
int sum=leftSum+rightSum+1; //加上中间节点
return sum;
}
int countNodes(TreeNode* root) {
return getNodes(root);
}
};
以上解法对于所有二叉树通用,下面解法只用于完全二叉树,即将完全二叉树分解为满二叉树
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
TreeNode* left=root->left;
TreeNode* right=root->right;
int leftHeight=0;
int rightHeight=0;
while(left){
leftHeight++;
left=left->left;
}
while(right){
rightHeight++;
right=right->right;
}
if(leftHeight==rightHeight){ //寻找满二叉树
return (2<<leftHeight)-1; //相当于2^h
}
return countNodes(root->left)+countNodes(root->right)+1;
}
};