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

513.找树左下角的值

代码随想录链接: 代码随想录 (programmercarl.com)

递归法:

这部分的回溯的精简代码没太想清楚,depth+1作为参数传入感觉很自然,但是没看出回溯是怎么隐藏进去的。

class Solution {
public:
    int findlv(TreeNode* root, int depth, int& result,int& maxdepth){
        if(root==NULL) return 0;
        if(root->left==NULL && root->right==NULL){
            if(depth>maxdepth){
                maxdepth=depth;
                result=root->val;
            }
            return result;
        }
        if(root->left){
            findlv(root->left,depth+1,result,maxdepth);
        }
        if(root->right){
            findlv(root->right,depth+1,result,maxdepth);
        }
        return result;
    }
    int findBottomLeftValue(TreeNode* root) {
        int maxdepth=-1;
        int depth=0;
        int result=0;
        return findlv(root,depth,result,maxdepth);
    }
};

迭代:

层序遍历的方法

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=NULL) que.push(root);
        int result;
        while(!que.empty()){
            int size=que.size();
            for(int i=0;i<size;i++){
                TreeNode* node=que.front();
                que.pop();
                if(i==0) result=node->val;
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return result;
    }
};

112. 路径总和

代码随想录链接:

感觉最后判断剩下的值和叶子节点的值是否相等比较好理解

class Solution {
public:
    bool sum(TreeNode* root, int target){
        if(root->left==NULL && root->right==NULL && target==root->val) return true;
        if(root->left==NULL && root->right==NULL) return false;

        if(root->left)
        {
            if(sum(root->left,target-root->val)) return true;
        }
        if(root->right){
            if(sum(root->right,target-root->val)) return true;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL) return false;
        return sum(root,targetSum);
    }
};

113. 路径总和 II

这个题感觉和 二叉树的所有路径 那道题有点像,没仔细看

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

代码随想录链接:代码随想录 (programmercarl.com)

中序是左中右,后序是左右中,所以后序的最后一个节点一定是根节点,根据这个切割中序数组,得到左中右之后在切割后续数组,递归下去。

一共分为六步:

  • 第一步:如果数组大小为零的话,说明是空节点了。

  • 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。

  • 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点

  • 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)

  • 第五步:切割后序数组,切成后序左数组和后序右数组

  • 第六步:递归处理左区间和右区间

切割时要注意循环不变量。

class Solution {
public:
    TreeNode* tra(vector<int>& inorder, vector<int>& postorder){
        if(postorder.size()==0) return NULL;

        int value=postorder[postorder.size()-1];
        TreeNode* root=new TreeNode(value); 

        if(postorder.size()==1) return root;
        
        int cut;
        for(cut=0;cut<inorder.size();cut++){
            if(inorder[cut]==value) break;
        }
        //左闭右开切割中序
        vector<int> leftinorder(inorder.begin(),inorder.begin()+cut);
        vector<int> rightinorder(inorder.begin()+cut+1,inorder.end());

        postorder.resize(postorder.size()-1);
        vector<int> leftpost(postorder.begin(),postorder.begin()+leftinorder.size());
        vector<int> rightpost(postorder.begin()+leftinorder.size(),postorder.end());

        root->left=tra(leftinorder,leftpost);
        root->right=tra(rightinorder,rightpost);

        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size()==0 || postorder.size()==0) return NULL;
        return tra(inorder,postorder);

    }
};

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

比较类似,没仔细看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值