代码随想录算法训练营第十六天|LeetCode 104. 二叉树的最大深度、LeetCode 111.二叉树的最小深度、LeetCode 222.完全二叉树的节点个数。

LeetCode 104. 二叉树的最大深度

题目链接:LeetCode 104.二叉树的最大深度

后序遍历:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL)
        return 0;
        int leftheight = maxDepth(root -> left);
        int rightheight = maxDepth(root -> right);
        int height = 1 + max(leftheight,rightheight);
        return height;
    }
};

思路:本题中要求二叉树的最大深度,即二叉树的高度;而二叉树的高度,可以用后序遍历的方法来求(父节点高度 = 子节点的高度 + 1)

小结:后序遍历可以实现自下至上的遍历,前序遍历可以实现自上至下的遍历;后序遍历可以求高度,前序遍历可以求深度;此题中用前序遍历更加复杂,更适合用后序遍历。

前序遍历:

class solution {
public:
    int result;
    void getdepth(treenode* node, int depth) {
        result = depth > result ? depth : result; 
        if (node->left == NULL && node->right == NULL) return ;
        if (node->left) {
            getdepth(node->left, depth + 1);
        }
        if (node->right) { 
            getdepth(node->right, depth + 1);
        }
        return ;
    }
    int maxdepth(treenode* root) {
        result = 0;
        if (root == 0) return result;
        getdepth(root, 1);
        return result;
    }
};

LeetCode 111.二叉树的最小深度

题目链接:LeetCode 111. 二叉树的最小深度

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == NULL)
        return 0;
        int leftheight = minDepth(root -> left);
        int rightheight = minDepth(root -> right);
        if(root -> left != NULL && root -> right == NULL)
        return 1 + leftheight; 
        else if(root -> left == NULL && root -> right != NULL)
        return 1 + rightheight; 
        else 
        return 1 + min(leftheight,rightheight);
    }
};

思路:与上一题思路大致相同,但需将左右高度最大值变为取最小值。但在本题中,最小深度是指根节点到最近子叶节点的节点数;故需加上特判,即根节点左叶节点为空但右叶节点不为空,返回右叶节点的高度加一,根;点右叶节点为空但右叶节点不为空,返回右叶节点的高度加一


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

题目链接:LeetCode 222. 完全二叉树的节点个数

普通二叉树解法:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == NULL)
        return 0;
        int leftnum = countNodes(root -> left);
        int rightnnum = countNodes(root -> right);
        return leftnum + rightnnum + 1;
    }
};

思路:通过后序遍历实现。

完全二叉树:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == NULL)
        return 0;
        TreeNode* left = root -> left;
        TreeNode* right = root -> right;
        int leftheight = 0,rightheight = 0;
        while(left)
        {
            left = left -> left;
            leftheight++;
        }
        while(right)
        {
            right = right -> right;
            rightheight++;
        }
        if(leftheight == rightheight)
        {
            return (2 << leftheight) - 1;//位运算,2向左移leftheight位,即2的leftheight次方(leftheight为满二叉树高度)
        }
        return countNodes(root -> left) + countNodes(root -> right) + 1;//后序遍历的简化版本
    }
};

思路:因满二叉树可以通过2^n - 1的公式来计算节点数,而完全二叉树的子树也可以为满二叉树。故我们先判断子树是否为满二叉树(记录左右子树节点个数,相等则为满二叉树),之后利用公式计算即可。若子树不为满二叉树,则与普通二叉树一致,通过后序遍历便可以实现对二叉树节点数的统计。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值