Day10(1)二叉树的最大深度(2)二叉树的最小深度(3)完全二叉树的节点个数(4) n叉树的最大深度

1. 二叉树的最大深度

解析
Leetcode104 二叉树的最大深度

参考文章

(2)思路

先序遍历 + 递归思路

(3)代码

/**
 * 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 maxDepth(TreeNode* root) {
        if(root == NULL)
            return 0;
        int leftdepth = maxDepth(root->left) + 1;
        int rightdepth = maxDepth(root->right) + 1;
        return leftdepth > rightdepth ? leftdepth : rightdepth;
    }
};

(4)总结

2. 二叉树的最小深度

(1)解析

Leetcode226 二叉树的翻转

参考文章

(2)思路

二叉树的左右子树为空时的判断

(3)代码

/**
 * 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 == NULL)
            return 0;
        int leftdepth = minDepth(root->left);
        int rightdepth = minDepth(root->right);
        if(root->left == NULL && root->right != NULL)
            return rightdepth + 1;
        else if(root->left != NULL && root->right == NULL)
            return leftdepth + 1;        
        else
            return 1 + (leftdepth > rightdepth ? rightdepth : leftdepth);
    }
};

(4)总结

3. 完全二叉树的节点个数

Leetcode222 完全二叉树的节点个数

参考文章

(2)思路

左子树+右子树+中间节点

(3)代码

/**
 * 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 countNodes(TreeNode* root) {
        if(root == NULL)
            return 0;
        int left = countNodes(root->left);
        int right = countNodes(root->right);
        return left + right + 1;
    }
};

(4)总结

4. n叉树的最大深度

Leetcode222 完全二叉树的节点个数

参考文章

(2)思路

1.分层迭代遍历
2.递归遍历

(3)代码

/*
// 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 == NULL)
            return 0;
        int depth = 0;
        queue<Node*> que;
        que.push(root);
        while(!que.empty())
        {
            depth += 1;
            int size = que.size();
            for(int i = 0; i < size; ++i)
            {
                Node *tmp = que.front();
                for(int j = 0; j < tmp->children.size(); ++j)
                    que.push(tmp->children[j]);
                que.pop();
            }
        }
        return depth;
    }
};

//递归遍历

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;
    }

};

(4)总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值