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

day18 3.4 二叉树第五天

513.找树左下角的值

链接: 513.找树左下角的值
思路:在树的最后一行找到最左边的值

class Solution {
public:
    int maxLen = INT_MIN;
    int maxleftValue;
    void traversal(TreeNode* root, int leftLen){
        if(root->left == NULL && root->right == NULL){
            if(leftLen >maxLen){
                maxLen = leftLen;
                maxleftValue = root->val;
            }
            return;
        }
        if(root->left){
            leftLen++;
            traversal(root->left,leftLen);
            leftLen--;//回溯

        }
        if(root->right){
            leftLen++;
            traversal(root->right,leftLen);
            leftLen--;//回溯
        }
    }
    int findBottomLeftValue(TreeNode* root) {
        traversal(root,0);
        return maxleftValue;

    }
};

112. 路径总和 113.路径总和ii

链接: 112. 路径总和
思路:递归函数什么时候要有返回值,什么时候没有返回值,特别是有的时候递归函数返回类型为bool类型。
并没有处理空结点,所以前中后序都行

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 targetSum) {
        if(root == NULL) return false;
        return traversal(root,targetSum - root->val);

    }
};

链接: 113.路径总和ii
思路:要遍历整个树,找到所有路径,所以递归函数不要返回值!

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    //递归函数不需要返回值,因为我们要遍历整个树
    void traversal(TreeNode* cur,int count){
        if(!cur->left && !cur->right && count == 0){//遇到了叶子结点找到了和为sum的路径
            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;//回溯

        }
        return;
    }

public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        result.clear();
        path.clear();
        if(root == NULL) return result;
        path.push_back(root->val);//把根节点放进路径
        traversal(root, targetSum - root->val);
        return result;

    }
};

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

第一步:如果数组大小为零的话,说明是空节点了。
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
第五步:切割后序数组,切成后序左数组和后序右数组
第六步:递归处理左区间和右区间

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 delimiterIndex;
        for(delimiterIndex = 0;delimiterIndex < inorder.size(); delimiterIndex++){
            if(inorder[delimiterIndex] == rootValue) break;
        }

        //切割中序数组
        //左闭右开区间:[0,delimiterIndex)
        vector<int> leftInorder(inorder.begin(),inorder.begin() + delimiterIndex);
        //[delimiterIndex + 1,end)
        vector<int> rightInorder(inorder.begin() + delimiterIndex + 1,inorder.end());


        //postorder 舍弃末尾元素
        postorder.resize(postorder.size() - 1);

        //切割后续数组
        //依然左闭右开,注意这里使用了左中序数组作为切割点
        //[0,leftInorder.size)
        vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
        //[leftInorder.size(),end)
        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);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值