代码随想录算法训练营Day13 |110.平衡二叉树,257. 二叉树的所有路径,404.左叶子之和,222.完全二叉树的节点个数

110.平衡二叉树

class Solution {
public:
    int getHeight(TreeNode* root){
        if(root == nullptr) return 0;

        int left = getHeight(root->left);
        if(left == -1) return -1;
        int right = getHeight(root->right);
        if(right == -1) return -1;
        return abs(left-right)>1 ? -1 : 1 + max(left,right);
    }
    bool isBalanced(TreeNode* root) {
        return getHeight(root) == -1 ? false : true;
    }
};

257. 二叉树的所有路径

std::string,可以直接使用加号(+)来连接字符串。

std::string重载了加号操作符,以支持字符串的连接。

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

404.左叶子之和

//迭代法
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        stack<TreeNode*>st;
        if(root == nullptr) return 0;
        st.push(root);
        int result = 0;
        while(!st.empty()){
            TreeNode* node = st.top();
            st.pop();
            if(node->left!=nullptr && node->left->left==nullptr && node->left->right==nullptr){
                result += node->left->val;
            }
            if(node->left)  st.push(node->left);
            if(node->right) st.push(node->right);
        }
        return result;
    }
};
 //递归法
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(root == nullptr) return 0;

        int left = sumOfLeftLeaves(root->left);
        if(root->left && !root->left->left && !root->left->right){
            left = root->left->val;
        }
        int right = sumOfLeftLeaves(root->right);
        int sum = left + right;
        return sum;
    }
};

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

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == nullptr) return 0;
        return 1+countNodes(root->left)+countNodes(root->right);
    }
};
class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*>que;
        int result = 0;
        if(root != nullptr) que.push(root);
        while(!que.empty()){
            int size = que.size();
            for(int i=0;i<size;i++){
                TreeNode* node = que.front();
                que.pop();
                result++;
                if(node->left)  que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值