代码随想录刷题第18天

代码随想录刷题第18天

513. 找树左下角的值

/**
 * 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 min_depth = -1;
    int result;
    void transation(TreeNode* root,int depth){
        if(!root->left && !root->right){
            if(depth > min_depth){
                min_depth = depth;
                result = root->val;
            }
            return;
        }
        if(root->left){
            depth++;
            transation(root->left, depth);
            depth--;
        }
        if(root->right){
            depth++;
            transation(root->right, depth);
            depth--;
        }
    }
    int findBottomLeftValue(TreeNode* root) {
        transation(root, 0);
        return result;
    }
};

深度和高度的概念要掌握,左下角的值就是左侧优先就好了!

112. 路径总和

/**
 * 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;
    
    void transation(TreeNode* root, int targetSum,int &result){
       // path.push_back(root->val);
        int tmp = targetSum - root->val;
        if(!root->left && !root->right){
            //叶子节点
            if(tmp == 0){
                result = 1;
            }
            return;
        }
        if(root->left){
            int tmp1 = targetSum - root->val;
            transation(root->left, tmp1,result);

        }
        if(root->right){
            int tmp1 = targetSum - root->val;
            transation(root->right, tmp1,result);

        }

    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr) {
            return false;
        }
        int result = -1;
        transation(root, targetSum,result);
        return result == -1 ? false : true;
    }
};

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

/**
 * 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* transation(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());
        // [leftInorder.size(), end)
        vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
                root->left = transation(leftInorder, leftPostorder);
        root->right = transation(rightInorder, rightPostorder);
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
     if (inorder.size() == 0 || postorder.size() == 0) return NULL;
       TreeNode *a = transation(inorder, postorder);
       return a;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值