*算法训练(leetcode)第十四天 | 513. 找树左下角的值、112. 路径总和、106. 从中序与后序遍历序列构造二叉树、D

513. 找树左下角的值

leetcode题目地址

层序遍历,记录每一层的第一个结点,遍历结束后最后一次记录的就是左下角的值。(注意题目要求不是最左侧的节点,是最后一层的最左侧结点)

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

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

112. 路径总和

leetcode题目地址

与之前做过的一道查找根节点到所有叶结点路径leetcode 257题)的题目的思路是一致的,只不过是把结点加入路径改为节点值加入和。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

// c++
/**
 * 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:
    void Order(TreeNode* root, int sum, int target, bool &flag){
        if(!root || flag) return;

        sum += root->val;

        if(!root->left && !root->right && sum==target){
            flag = true;
            return;
        }
        if(!flag && root->left) Order(root->left, sum, target, flag);
        if(!flag && root->right) Order(root->right, sum, target, flag);
        
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        int sum = 0;
        bool flag = false;
        Order(root, sum, targetSum, flag);
        return flag;
    }
};

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

leetcode题目地址

本题属于一想就会,一写就废。代码实现起来相对复杂…
思路来源

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

// c++
/**
 * 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* createTree(vector<int>& inorder, 
                        vector<int>& postorder, 
                        int inleft, int inright,
                        int postleft, int postright){
        
        if(postleft > postright) return NULL;
        TreeNode* root = new TreeNode(postorder[postright]);
        if(postright-postleft == 0) return root;

        int splitIdx;
        for(splitIdx=0; splitIdx<=inright-inleft; splitIdx++)
            if(inorder[inleft+splitIdx] == postorder[postright]) break;
        
        root->left = createTree(inorder, postorder, inleft, inleft+splitIdx-1, postleft, postleft+splitIdx-1);
        root->right = createTree(inorder, postorder, inleft+splitIdx+1, inright, postleft+splitIdx, postright-1);
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size() == 0) return NULL;
        TreeNode* root;
        // int size = inorder.size();
        root = createTree(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
        return root;
    }
};
  • 24
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值