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

二叉树


一、找树左下角的值

层序遍历,最后一层的第一个

/**
 * 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);
        vector<int>result;
        int val;
        while (!que.empty()) {
            int len = que.size();
            for (int i = 0; i < len; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (i == 0) val = node->val;
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return val;
    }
};

二、 路径总和

1.递归,终止条件为判断是否是目标值,如果是则返回true,不是则继续遍历,涉及回溯到上一个父节点
2.迭代,相对来说要好理解一些,两个栈,一个存放当前遍历节点,另一个存放当前遍历节点及之前节点的数值和,如果满足条件则退出

/**
 * 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 traversal(TreeNode* root, int targetSum) {
        //终止条件
        if (!root->left && !root->right && targetSum == 0) return true;
        if (!root->left && !root->right ) return false;
        if (root->left) {
            targetSum -= root->left->val;
            if (traversal(root->left, targetSum)) return true;
            targetSum += root->left->val;
        }
        if (root->right) {
            targetSum -= root->right->val;
            if (traversal(root->right, targetSum)) return true;
            targetSum += root->right->val;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) return false;
        return traversal(root, targetSum - root->val);
    }
};

三、路径总和ii

1.递归,需要再加一个记录路径的vector,同样需要回溯到上一个父节点
2.迭代,有点麻烦

/**
 * 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:
    vector<int>path;
    vector<vector<int>>result;
    void traversal (TreeNode* root, int count) {
        //终止条件
        if (!root->left && !root->right && count == 0){
            result.push_back(path);
            return;
        } 
        if (!root->left && !root->right) return;

        if (root->left) {
            path.push_back(root->left->val);
            count -= root->left->val;
            traversal(root->left, count);    // 递归
            count += root->left->val;        // 回溯
            path.pop_back(); 
        }

        if (root->right) {
            path.push_back(root->right->val);
            count -= root->right->val;
            traversal(root->right, count);   // 递归
            count += root->right->val;       // 回溯
            path.pop_back();
        }
        return ;
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) return result;
        path.push_back(root->val);
        traversal(root, targetSum-root->val);
        return result;
    }
};

四、从前序与中序遍历序列构造二叉树

利用前中序的节点排列特点

/**
 * 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* traversal(vector<int>& inorder, vector<int>& postorder) {
        //确定终止条件
        if (postorder.size() == 0) return NULL;

        //1.后序的最后一个元素是根节点
        int rootValue = postorder[postorder.size() - 1];
        TreeNode* root = new TreeNode(rootValue);

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

        //切割坐序列
        int index;
        for (index = 0; index < inorder.size(); index++) {
            if (inorder[index] == rootValue) {
                break;
            }
        }
        vector<int>inleft(inorder.begin(), inorder.begin() + index);
        vector<int>inright(inorder.begin() + index + 1, inorder.end());

        //切割右序列
        postorder.resize(postorder.size() - 1);
        vector<int>postleft(postorder.begin(), postorder.begin() + inleft.size() );
        vector<int>postright(postorder.begin() + inleft.size(), postorder.end());
        
        //连接并递归
        root->left = traversal(inleft, postleft);
        root->right = traversal(inright, postright);
        
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        //从中序和后序遍历构造二叉树
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return traversal(inorder, postorder);
    }
};

总结

路径总和递归有点绕,多写多练,构造二叉树有点思路,但不多
学习时间120min。
学习资料:《代码随想录》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值