513. 找树左下角的值、112. 路径总和、113. 路径总和II、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

代码随想录算法训练营第1天|513. 找树左下角的值、112. 路径总和、113. 路径总和II、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

513. 找树左下角的值

题目链接

提交代码

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        int result;
        queue<TreeNode*> que;
        if(root == nullptr)
            return result;
        que.push(root);
        while(!que.empty())
        {
            int size = que.size();
            vector<int> vec;
            while(size--)
            {
                TreeNode* node = que.front();
                que.pop();
                vec.push_back(node -> val);
                if(node -> left != nullptr)
                    que.push(node -> left);
                if(node -> right != nullptr)
                    que.push(node -> right);
            }   
            result = vec[0];
        }
        return result;
    }
};

递归法

class Solution {
public:
    int maxDepth = 0;
    int result = 0;
    void findBottomLeftValue(TreeNode* node, int depth)
    {
        if(node == nullptr) return;
        if(depth > maxDepth)
        {
            maxDepth = depth;
            result = node -> val;
        }
        findBottomLeftValue(node -> left, depth + 1);
        findBottomLeftValue(node -> right, depth + 1);
    }
    int findBottomLeftValue(TreeNode* root) {
        int depth = 1; 
        findBottomLeftValue(root, depth);
        return result;
    }
};

112. 路径总和

题目链接

提交代码(方法)

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr) return false;
        if(!root -> left && !root -> right && root -> val - targetSum == 0) return true;
        int leftSum = hasPathSum(root -> left, targetSum - root ->val);
        int rightSum = hasPathSum(root -> right, targetSum - root -> val);
        return leftSum || rightSum;
    }
};

113. 路径总和II

题目链接

提交代码(方法)

class Solution {
public:
    vector<vector<int>> result;
    void pathSum(TreeNode* node, int targetSum, vector<int> path, vector<vector<int>>& result)
    {
        if(node == nullptr) return ;
        path.push_back(node -> val);
        if(!node -> left && !node -> right && node -> val - targetSum == 0)
        {
            result.push_back(path);
            //path.pop_back();
            return;
        }
        pathSum(node -> left, targetSum - node ->val, path, result);
        //path.pop_back();
        pathSum(node -> right, targetSum - node -> val, path, result); 
        //path.pop_back();
        return;
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<int> path;
        vector<vector<int>> result;
        pathSum(root, targetSum, path, result);
        return result;
    }
};

106.从中序与后序遍历序列构造二叉树

题目链接

提交代码(方法)

class Solution {
private:
    TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
         if(inorder.size() == 0) return nullptr;
         int size = postorder.size();
         int rootVal = postorder[size - 1];
         TreeNode* root = new TreeNode(rootVal);
         if(size == 1) return root;
         int rootInOrder = 0;
         for(;rootInOrder < inorder.size(); rootInOrder++)
             if(inorder[rootInOrder] == rootVal) break;
         vector<int> leftInorder(inorder.begin(), inorder.begin() + rootInOrder);
         vector<int> rightInorder(inorder.begin() + rootInOrder + 1, inorder.end());

         postorder.pop_back();

         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);
    }
};

105.从前序与中序遍历序列构造二叉树

题目链接

提交代码(方法)

class Solution {
public:
    TreeNode* traversal(vector<int>& postorder, vector<int>& inorder)
    {
        if(postorder.size() == 0) return nullptr;
         int size = postorder.size();
         int rootVal = postorder[0];
         TreeNode* root = new TreeNode(rootVal);
         if(size == 1) return root;
         int rootInOrder = 0;
         for(;rootInOrder < inorder.size(); rootInOrder++)
             if(inorder[rootInOrder] == rootVal) break;
         vector<int> leftInorder(inorder.begin(), inorder.begin() + rootInOrder);
         vector<int> rightInorder(inorder.begin() + rootInOrder + 1, inorder.end());

         //postorder.pop_back();

         vector<int> leftPostorder(postorder.begin() + 1, postorder.begin() + leftInorder.size() + 1);
         vector<int> rightPostorder(postorder.begin() + leftInorder.size() + 1, postorder.end());
        
         root -> left = traversal(leftPostorder, leftInorder );
         root -> right = traversal(rightPostorder, rightInorder);

         return root;
        
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size() == 0) return nullptr;
        return traversal(preorder, inorder);
    }
};

总结

                     日期: 2023 年 4 月 1 日
              学习时长: 0 h 30 m
                     难度: ★ \bigstar
累计完成题目数量: 56
距离目标还有数量: 244
                      小结:

  1. 第三道题的path不用引用
  2. 最后一道题是倒数第二道题改了改,所以变量没成没改

你可能感兴趣的:(leetcode,算法,c++)