代码随想录day18 补

513

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

递归法

class Solution {
public:
    void dfs(TreeNode* cur,int height, int& curHeight,int& res ){
        if(cur == NULL){
            return;
        }
        height++;
        dfs(cur->left,height,curHeight,res);
        dfs(cur->right,height,curHeight,res);
        if(height > curHeight){
            curHeight = height;
            res = cur->val;
        }
    }
    int findBottomLeftValue(TreeNode* root) {
        int res, curHeight = 0;
        dfs(root, 0, curHeight,res);
        return res;
    }
};

112

class Solution {
public:
    void travel(TreeNode* cur,vector<int>& path,int targetSum, bool& res){
        path.push_back(cur->val);
        if(cur->left == NULL && cur->right == NULL){
            int total = accumulate(path.begin(), path.end(), 0);
            if(total == targetSum){
                res = true;
                return;
            }
        }
        if(cur->left){
            travel(cur->left,path,targetSum,res);
            path.pop_back();
        }
        if(cur->right){
            travel(cur->right,path,targetSum,res);
            path.pop_back();
        }
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        bool res = false;
        vector<int> path;
        if(root == NULL) return res;
        travel(root,path,targetSum,res);
        return res;
    }
};

113 路经总和


class Solution {
public:
    void travel(TreeNode* cur,vector<int>& path,int targetSum,vector<vector<int>>& res ){
        path.push_back(cur->val);
        if(cur->left == NULL && cur->right == NULL){
            int total = accumulate(path.begin(), path.end(), 0);
            if(total == targetSum){
                res.push_back(path);
                return;
            }
        }
        if(cur->left){
            travel(cur->left,path,targetSum,res);
            path.pop_back();
        }
        if(cur->right){
            travel(cur->right,path,targetSum,res);
            path.pop_back();
        }
    }


    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<vector<int>> res ;
        vector<int> path;
        if(root == NULL) return res;
        travel(root,path,targetSum,res);
        return res;
    }
};

106


class Solution {
private:
    TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
        if (postorder.size() == 0) return NULL;

        int rootValue = postorder[postorder.size() - 1];
        TreeNode* root = new TreeNode(rootValue);

        if (postorder.size() == 1) return root;

        int delimiterIndex;
        for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
            if (inorder[delimiterIndex] == rootValue) break;
        }

        vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
        vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );

        postorder.resize(postorder.size() - 1);

  
        vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
        vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());

        root->left = traversal(leftInorder, leftPostorder);
        root->right = traversal(rightInorder, rightPostorder);

        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return traversal(inorder, postorder);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值