代码随想录 第六章 二叉树 part04 找树左下角的值 路径总和 从中序与后序遍历序列构造二叉树

找树左下角的值

递归

/**
 * 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 d=INT_MIN;
    int result;
    int findBottomLeftValue(TreeNode* root) {
        read(root,0);
        return result;
    }

    void read(TreeNode* node, int dtmp){
        if(node==NULL) return;
        if(dtmp>d){
            d=dtmp;
            result=node->val;
        }
        read(node->left,dtmp+1);
        read(node->right,dtmp+1);
    }
};

迭代

/**
 * 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*>q;
        int result;
        q.push(root);
        while(!q.empty()){
            int s=q.size();
            result=q.front()->val;
            while(s>0){
                TreeNode* node=q.front();
                if(node->left!=NULL) q.push(node->left);
                if(node->right!=NULL) q.push(node->right);
                q.pop();
                s--;
            }
        }
        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:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL) return false;
        return read(root,0,targetSum);
    }

    bool read(TreeNode* node, int sum, int targetSum){
        if(node==NULL) return false;
        sum+=node->val;
        if(node->left==NULL&&node->right==NULL&&sum==targetSum) return true;
        if(node->left==NULL&&node->right==NULL) return false;
        if(!read(node->left,sum,targetSum)){
            if(!read(node->right,sum,targetSum)){
                return false;
            }
        }
        return true;
    }
};

113. 路径总和ii

/**
 * 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<vector<int>>result;
    vector<int>path;
    int sum=0, tsum=0;
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        tsum=targetSum;
        read(root);
        return result;
    }
    void read(TreeNode* node){
        if(node==NULL) return;
        sum=sum+node->val;
        path.push_back(node->val);
        if(node->left==NULL&&node->right==NULL&&sum==tsum){
            result.push_back(path);
        }else{
            read(node->left);
            read(node->right);
        }
        sum=sum-node->val;
        path.pop_back();
    }
};

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

/**
 * 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* buildTree(vector<int>& inorder, vector<int>& postorder) {
        TreeNode* root=bd(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
        return root;
    }
    TreeNode* bd(vector<int>& inorder, vector<int> postorder, int il, int ir, int pl, int pr){
        TreeNode* node=new TreeNode(postorder[pr]);
        if(pl<pr){
            int rt;
            for(rt=il;postorder[pr]!=inorder[rt];rt++);
            if(rt!=il) node->left=bd(inorder,postorder,il,rt-1,pl,pl+(rt-il)-1);
            if(rt!=ir) node->right=bd(inorder,postorder,rt+1,ir,pl+(rt-il),pr-1);
        }
        return node;
    }
};

要注意的就是确认索引范围,确保索引范围内是同一颗子二叉树的中序与后序遍历结果,再根据后序遍历的最后一个元素也就是子二叉树的根,找到再中序遍历中根出现的位置,再确定新的索引范围,直到索引范围内只有一个元素。

代码随想录 第六章 二叉树 part04

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值