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

513.找树左下角的值

class Solution {//递归
    int res=0;
    int max_depth = 0;
    void traversal(TreeNode* root, int depth)
    {
        
        if(!root->left&&!root->right)
        {
            if(depth>max_depth)
            {
                max_depth  = depth;
                res = root->val;
            }
        }
        if(root->left) traversal(root->left,depth+1);
        if(root->right) traversal(root->right, depth+1);
        
    }
public:
    int findBottomLeftValue(TreeNode* root) {
        traversal(root, 1);
        return res;
    }
};
class Solution {//迭代
  
public:
    int findBottomLeftValue(TreeNode* root) {
         queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        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 hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL) return false;//如果是空节点直接返回false
        targetSum -= root->val; //减去当前节点的值
        if(!root->left && !root->right)//如果是叶子节点,判断target是否减到0了,如果没有就返回false;
        {
            if(targetSum==0) return true;
            else return false;
        } 
        if(root->left)//如果左子树存在就可以玩左走
        {
            bool left = hasPathSum(root->left, targetSum);  
            if(left==true) return true;//如果左子树返回了true说明存在路径,直接返回true右子树可以不看了
        }
        if(root->right){
            bool right = hasPathSum(root->right, targetSum);
            if(right==true) return true;//如果右子树返回true也可直接返回true
        }
        return false;//如果左右子树都没有路径,就返回false
    }
};

113.路径总和ii

class Solution {
    vector<vector<int>> res;
    vector<int>path;
    void traversal(TreeNode* root, int targetSum)
    {
        path.push_back(root->val);
        targetSum -= root->val;
        if(!root->left&&!root->right)
        {
            if(targetSum == 0) res.push_back(path);
        }
        if(root->left){
           traversal(root->left, targetSum);
           path.pop_back();
        }
        if(root->right) 
        {
            traversal(root->right, targetSum);
            path.pop_back();
        }
        return;
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(root==NULL) return res;
        traversal(root, targetSum);
        return res;
    }
};

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

【文章讲解】

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

【文章讲解】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值