二叉树的深度&高度

104. 二叉树的最大深度

方法:递归(后序遍历)

class Solution {
private:
    int getDep(TreeNode*cur) {
        if (cur == NULL) return 0;
        int l = getDep(cur->left);
        int r = getDep(cur->right);
        int res = max(l, r) + 1;
        return res;
    }
public:
    int maxDepth(TreeNode* root) {
        return getDep(root);
    }
};

$时间复杂度O(n),空间复杂度O(h),h为树的高度

方法:递归(前序遍历)

class Solution {
private:
    int res;
    void getDep(TreeNode* cur, int dep) {
        res = res > dep ? res : dep;
        if (!cur->left && !cur->right) return;

        if (cur->left) getDep(cur->left, dep+1);

        if (cur->right) getDep(cur->right, dep+1);
    }
public:
    int maxDepth(TreeNode* root) {
        res = 0;
        if (root == NULL) return res;
        getDep(root, 1);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(h),h为树的高度

方法:bfs(层次遍历)

/**
 * 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) {
        queue<TreeNode*> q;
        int dep = 0;
        if (root != NULL) q.push(root);
        while (!q.empty()) {
            int n = q.size();
            dep++;
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
            }
        }
        return dep;
    }
};

$时间复杂度O(n),空间复杂度O(n)

111. 二叉树的最小深度

方法:递归(前序遍历)

/**
 * 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 {
private:
    int res;
    void getDep(TreeNode* cur, int dep) {
        if (!cur->left && !cur->right) {
            res = min(res, dep);
            return ;
        }

        if (cur->left) getDep(cur->left, dep+1);

        if (cur->right) getDep(cur->right, dep+1);

        return ;
    }
public:
    int minDepth(TreeNode* root) {
        
        if (root == NULL) return 0;
        res = INT_MAX;
        getDep(root, 1);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(h),h为树的高度

方法:递归(后序遍历)

/**
 * 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 {
    int getDep(TreeNode* nod) {
        if (nod == NULL) return 0;
        int l = getDep(nod->left), r = getDep(nod->right);
        if (nod->left == NULL && nod->right) return 1 + r;
        if (nod->right == NULL && nod->left) return 1 + l;
        int res = min(l, r) + 1;
        return res;
    }
public:
    int minDepth(TreeNode* root) {
        return getDep(root);
    }
};

$时间复杂度O(n),空间复杂度O(h),h为树的高度

方法:bfs(层次遍历)

/**
 * 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) {
        queue<TreeNode*> q;
        if (root != NULL) q.push(root);

        int dep = 0;
        while (!q.empty()) {
            int n = q.size();
            ++dep;
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
                if (!nod->left && !nod->right) return dep;
            }
        }
        return dep;
    }
};

$时间复杂度O(n),空间复杂度O(n)

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

方法:递归

/**
 * 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 {
private:
    int now = 0;
    int getVal(TreeNode* cur) {
        if (cur == NULL) return 0;
        int l =  getVal(cur->left);
        int r = getVal(cur->right);
        int now = l + r + 1;
        return now;
    }
public:
    int countNodes(TreeNode* root) {
        if (root == NULL) return 0;
        return getVal(root);
    }
};

$时间复杂度O(n),空间复杂度O(h),h为树的高度

方法:bfs(层次遍历)

/**
 * 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) {
        queue<TreeNode*> q;
        if (root != NULL) q.push(root);

        int res = 0;

        while (!q.empty()) {
            int n = q.size();
            res += n;
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值