Code Caprice Algorithm Training Camp Day 17|110. Balanced Binary Tree| 257. All Paths of Binary Tree| 404. Sum of Left Leaves

110. Balanced Binary Tree

/**
 * 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 {
    
    
    bool flag=true;
    int dfs(TreeNode*node,int depth){
    
    
        if(node==nullptr)return 0;
        int left=dfs(node->left,depth+1);
        int right=dfs(node->right,depth+1);
        if(abs(left-right)>1)flag=false;
        return max(left,right)+1;
        
    }

public:
    bool isBalanced(TreeNode* root) {
    
    
        int a=dfs(root,0);
        return flag;
    }
};

257. All Paths of a Binary Tree

/**
 * 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 {
    
    
    vector<string>res;
    void travesal(TreeNode* root,vector<int>&vec){
    
    
         if(root->left==nullptr&&root->right==nullptr){
    
    
             string s;
             for(int i=0;i<vec.size();i++){
    
    
                 if(i!=vec.size()-1){
    
    
                     s+=to_string(vec[i])+"->";
                 }else{
    
    
                     s+=to_string(vec[i]);
                 }
                 
             }
             res.push_back(s);
             return;
         }
         if(root->left!=nullptr){
    
    
             vec.push_back(root->left->val);
             travesal(root->left,vec);
             vec.pop_back();
         }
         if(root->right!=nullptr){
    
    
             vec.push_back(root->right->val);
             travesal(root->right,vec);
             vec.pop_back();
         }
     }


public:
    vector<string> binaryTreePaths(TreeNode* root) {
    
    
        vector<int>vec;
        vec.push_back(root->val);
        travesal(root,vec);
        return res;
    }
};

404. The sum of the left leaves

/**
 * 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 {
    
    
    int res=0;
    void travesal(TreeNode* root,bool isleft){
    
    
        if(root->left==nullptr&&root->right==nullptr&&isleft){
    
    
            res+=root->val;
            return ;
        }
        if(root->left!=nullptr)travesal(root->left,true);
        if(root->right!=nullptr)travesal(root->right,false);
    }
public:
    int sumOfLeftLeaves(TreeNode* root) {
    
    
        travesal(root,false);
        return res;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43541510/article/details/132224483