LeetCodeDay15二叉树

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


LeetCode222.完全二叉树的节点数

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(!root)return 0;
        else{
            return 1 + countNodes(root->left) + countNodes(root->right);
        } 
    }
};

LeetCode110.平衡二叉树

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(!root)return true;
        else{
            if(abs(maxDepth(root->left) - maxDepth(root->right)) > 1)
                return false;
            else
                return isBalanced(root->left) && isBalanced(root->right);
        }
    }
    int maxDepth(TreeNode* root){
        if(!root)return 0;
        else{
            return 1 + max(maxDepth(root->left),maxDepth(root->right));
        }
    }
};

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

LeetCode257.二叉树的所有路径

class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path = "";
        constructPath(root, path, result);
        return result;
    }
    void constructPath(TreeNode* root, string path, vector<string>&result){
        if(root != nullptr){
            path += to_string(root->val);
            if(root->left == nullptr && root->right == nullptr){
                result.push_back(path);
            }else{
                path += "->";
                constructPath(root->right, path, result);
                constructPath(root->left, path, result);
            }
        }
    }
};

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值