代码随想录算法训练营第十六天|LeetCode104 二叉树的最大深度、LeetCode559 n叉树的最大深度、LeetCode111 二叉树的最小深度、LeetCode222 完全二叉树的节点个数

104.二叉树的最大深度

思路:递归法:后序遍历,先确定递归函数的参数和返回类型,然后找到递归函数的终止条件,在确定单层递归的逻辑,左右中。注意深度和高度的关系。后序遍历的最大高度即为二叉树的最大深度。

class Solution {
public:
    int getheight(TreeNode* root)
    {
        if(root==NULL)
        {
            return 0;
        }
        int leftheight = getheight(root->left);
        int rightheight = getheight(root->right);
        int result = 1+max(leftheight,rightheight);
        return result;
    }
    int maxDepth(TreeNode* root) {
        //递归的思想 后序遍历 高度及深度
        return getheight(root);
    }
};

迭代法:利用层序遍历,每遍历一层,深度++,当队列清空时输出深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        queue<TreeNode*> que;
        if(root!=NULL)
        {
            que.push(root);
        }
        while(!que.empty())
        {
            depth++;
            int size =que.size();
            while(size--)
            {
                TreeNode* node = que.front();
                que.pop();
                if(node->left)
                {
                    que.push(node->left);
                }
                if(node->right)
                {
                    que.push(node->right);
                }
            }

        }
        return depth;
    }
};

559.n叉树的最大深度

思路:原理同二叉树,只不过在判断深度的时候要把n个子节点全部判断。

递归法:

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

111.二叉树的最小深度

思路:要注意叶子节点的概念,叶子节点是指没有子节点的节点。

递归法:后序遍历,先确定递归函数的参数和返回类型,在确定递归的终止条件,再确定单层递归的逻辑。左右中。如果子节点一边为空另一边不为空,当前最小深度为1+不为空的节点深度,如果两边都为空,当前深度为1,如果两边都不为空,当前最小深度为1+更小的节点深度。

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+rightheight;
        }
        else if(root->left!=NULL&&root->right==NULL)
        {
            return 1+leftheight;
        }
        int result = 1+min(leftheight,rightheight);
        return result;
    }
};

迭代法:层序遍历,按层挨个节点遍历,逐层深度++,当遇见节点的左右子节点都为空时,返回深度值。(这里没弄明白为什么while后还要return,不是在while内就可以return出来了吗。)

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth = 0;
        if(root!=NULL)
        {
            que.push(root);
        }
        else
        {
            return depth;
        }
        while(!que.empty())
        {
            depth++;
            int size = que.size();
            while(size--)
            {
                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)
                {
                    return depth;
                }
            }
        }
        return depth;
    }
};

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

思路:递归法,先判断递归函数的参数和返回类型,再判断递归的终止条件,再判断单层递归的逻辑。这里可以直接用后序遍历。当前节点数量为子节点数量和+1。

class Solution {
public:
    int countNodes(TreeNode* root) {
        //后序遍历 递归
        if(root==NULL)
        {
            return 0;
        }
        int leftnum = countNodes(root->left);
        int rightnum = countNodes(root->right);
        int result = leftnum+rightnum+1;
        return result;
    }
};

根据完全二叉树的特性,可以简化遍历,少遍历一些节点,即如果子树是一个满二叉树,只需要得到它的深度,通过2^深度-1就可以计算出子树的节点个数。但此时递归函数的终止条件需要额外加上,判断子树的左右深度是否相同,相同时返回节点个数。

class Solution {
public:
    int countNodes(TreeNode* root) {
        //  利用完全二叉树性质 递归
        if(root== NULL)
        {
            return 0;
        }
        int leftdepth=0;
        int rightdepth=0;
        TreeNode* leftnode = root->left;
        TreeNode* rightnode = root->right;
        while(leftnode)
        {
            leftnode=leftnode->left;
            leftdepth++;
        }
        while(rightnode)
        {
            rightnode=rightnode->right;
            rightdepth++;
        }
        if(leftdepth==rightdepth)
        {
            return (2<<leftdepth)-1;
        }
        int leftnum = countNodes(root->left);
        int rightnum =countNodes(root->right);
        int result = leftnum+rightnum+1;
        return result;
    }
};

收获:

1、熟练使用递归法后序遍历,要先判断递归函数的参数和返回类型,确定递归函数的终止条件,确定单层递归的逻辑。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值