代码随想录刷题第17天

代码随想录刷题第17天

平衡二叉树

自己的解法:挨个计算,复杂度n^2

/**
 * 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 transation(TreeNode *root){
        if(root == nullptr) return 0;
        int left = transation(root->left);
        int right = transation(root->right);
        int result = 1 + max(left,right );
        return result;
    }
    bool isBalanced(TreeNode* root) {
        if(root == nullptr) return true;
        int left = transation(root->left);
        int right = transation(root->right);
        bool b_left=isBalanced(root->left);
        bool b_right = isBalanced(root->right);
        if(abs(left - right) <= 1 && (b_left && b_right)) return true;
        return false;
    }
};

优化的解法:

/**
 * 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 transation(TreeNode *root){
        if(root == nullptr) return 0;
        int left = transation(root->left);
        if(left == -1) return -1;
        int right = transation(root->right);
        if(right == -1) return -1;
        if(abs(left -right) > 1) return -1;
        else{
            return  1 + max(left, right);
        }
    }
    bool isBalanced(TreeNode* root) {
        return transation(root) == -1 ? false:true;
    }
};

时间复杂度为logn!!!

二叉树的所有路径

/**
 * 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:
    vector<int> path;
    void transation(TreeNode* root,vector<string> &result,vector<int> &path){
        path.push_back(root->val);
        if(root->left == nullptr && root->right == nullptr){
            //叶子节点
            string tmp;
            for(int i = 0; i < path.size() - 1; i++){
                tmp += to_string(path[i]);
                tmp += "->";
            }
            tmp += to_string(path[path.size()-1]);
            result.push_back(tmp);
            
            return;
        }
        if(root->left){
            transation(root->left, result, path);
            path.pop_back();
            
        }
        if(root->right){
            transation(root->right, result, path);
            path.pop_back();
        }

    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        transation(root, result, path);
        return result;
    }
};

左叶子之和

/**
 * 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 result = 0;
    void transation(TreeNode*root){
        if(!root) return;
        if(root->left == nullptr && root->right == nullptr)return ;
        if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr){
            result+=root->left->val;
            //return ;
        }
        transation(root->left);
        transation(root->right);

    }
    int sumOfLeftLeaves(TreeNode* root) {
       // if(root == nullptr) return 0;
        transation(root);
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值