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

513.找树左下角的值

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台513. 找树左下角的值 - 力扣(LeetCode)

视频讲解:怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值_哔哩哔哩_bilibili

文章讲解:代码随想录 (programmercarl.com)

看到说迭代法简单一些,就自己用迭代法做了,之后有空再来补上递归法

/**
 * 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 findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        que.push(root);
        TreeNode* cur = root;
        vector<int> vec;
        while(!que.empty()){
            int size = que.size();
            vec.clear();
            while(size--){
                cur = que.front();
                que.pop();
                vec.push_back(cur->val);
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return vec[0];
    }
};

路径总和

112. 路径总和

题目链接:112. 路径总和 - 力扣(LeetCode)

视频讲解:拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和_哔哩哔哩_bilibili

文章讲解:代码随想录 (programmercarl.com)

还是不太理解

/**
 * 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 {
private:
    bool traversal(TreeNode* cur, int count) {
        if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
        if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回

        if (cur->left) { // 左
            count -= cur->left->val; // 递归,处理节点;
            if (traversal(cur->left, count)) return true;
            count += cur->left->val; // 回溯,撤销处理结果
        }
        if (cur->right) { // 右
            count -= cur->right->val; // 递归,处理节点;
            if (traversal(cur->right, count)) return true;
            count += cur->right->val; // 回溯,撤销处理结果
        }
        return false;
    }

public:
    bool hasPathSum(TreeNode* root, int sum) {
        if (root == NULL) return false;
        return traversal(root, sum - root->val);
    }
};

113. 路径总和Ⅱ

题目链接:113. 路径总和 II - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

不太理解+1

/**
 * 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 {
private:
    vector<int> path;
    vector<vector<int>> result;
    void traversal(TreeNode* cur, int count){
        if(!cur->left && !cur->right && count==0){
            result.push_back(path);
            return;
        }
        if(!cur->left && !cur->right) return;
        if(cur->left){
            path.push_back(cur->left->val);
            count -= cur->left->val;
            traversal(cur->left, count);
            count += cur->left->val;
            path.pop_back();
        }
        if(cur->right){
            path.push_back(cur->right->val);
            count -= cur->right->val;
            traversal(cur->right, count);
            count += cur->right->val;
            path.pop_back();
        }
        return;
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        path.clear();
        result.clear();
        if(root==NULL) return result;
        path.push_back(root->val); // 把根节点放进路径
        traversal(root, targetSum - root->val);
        return result;
    }
};

遍历序列构造二叉树

只有前序和后序无法唯一构造二叉树

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

题目链接:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

视频讲解:坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树_哔哩哔哩_bilibili

文章讲解:代码随想录 (programmercarl.com)

【解题步骤】

  1. 后序数组为0,空节点
  2. 后序数组最后一个元素为节点元素
  3. 寻找中序数组位置作为切割点
  4. 切割中序数组
  5. 切割后序数组
  6. 递归处理左右区间
/**
 * 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 {
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 index = 0;
        for(index=0; index<inorder.size(); index++){
            if(inorder[index]==rootValue) break;
        }
        cout << index << endl;
        // 切割中序数组
        vector<int> leftInorder(inorder.begin(), inorder.begin()+index);  // 左中序,注意vector<int>的函数构造就是左闭右开的,所以其实左区间不包括index,刚好开掉了所以不用-1
        vector<int> rightInorder(inorder.begin()+index+1, inorder.end());  // 右中序

        // 拿着中序数组里左区间的大小,切割后序数组的左区间
        vector<int> leftPostOrder(postorder.begin(), postorder.begin()+index);  // 左后序 
        vector<int> rightPostorder(postorder.begin()+index, postorder.end()-1);  // 右后序

        root->left = traversal(leftInorder, leftPostOrder);
        root->right = traversal(rightInorder, rightPostorder);
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        TreeNode* root = traversal(inorder, postorder);
        return root;
    }
};

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

题目链接:105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

自己搞定啦

/**
 * 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:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size()==0) return NULL;
        int rootValue = preorder[0];
        TreeNode* root = new TreeNode(rootValue);
        if(preorder.size()==1) return root;
        int index = 0;
        for(index=0; index<inorder.size(); index++){
            if(inorder[index]==rootValue) break;
        }
        // 切割中序数组
        vector<int> leftInorder(inorder.begin(), inorder.begin()+index);
        vector<int> rightInorder(inorder.begin()+index+1, inorder.end());
        // 切割前序数组
        vector<int> leftPreorder(preorder.begin()+1, preorder.begin()+index+1);  // 保持长度与左中序数组一致
        vector<int> rightPreorder(preorder.begin()+index+1, preorder.end());
        // 递归
        root->left = buildTree(leftPreorder, leftInorder);
        root->right = buildTree(rightPreorder, rightInorder);
        return root;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值