二叉树part5

513.找树左下角的值

思路对了一半,本题可以用迭代法和递归法来解题。

递归法

设置记录最大层数和记录当前遍历层数的变量。
遇到的问题之一就是“如何记录节点的最大深度”,两个变量就可以轻松解决。

class Solution {
public:
    int maxDepth = INT_MIN;
    int result;
    void traversal(TreeNode* root, int depth) {
        if (root->left == NULL && root->right == NULL) {
            if (depth > maxDepth) {
                maxDepth = depth;
                result = root->val;
            }
            return;
        }
        if (root->left) {
            depth++;
            traversal(root->left, depth);
            depth--; // 回溯
        }
        if (root->right) {
            depth++;
            traversal(root->right, depth);
            depth--; // 回溯
        }
        return;
    }
    int findBottomLeftValue(TreeNode* root) {
        traversal(root, 0);
        return result;
    }
};

只要保证“左”在前就行,这样就可以先遍历左边的节点,上面还有回溯算法的影子,这样就可以保证depth的准确

迭代法

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=nullptr)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:
    int Sum=0;vector<int>vec;
    vector<int>vec1;
    vector<int> sumtarget(TreeNode* root)
    {
        if(root==nullptr)return vec; 
        
        if(root->left==nullptr&&root->right==nullptr)
        {
            Sum=Sum+root->val;
            vec.push_back(Sum);
            Sum=Sum-root->val;
            return vec; 
        }
       
        if(root->left)
        {
            Sum=Sum+root->val;
            sumtarget(root->left);
            Sum=Sum-root->val;
        }
        if(root->right)
        {
            Sum=Sum+root->val;
            sumtarget(root->right);
            Sum=Sum-root->val;
        }
        return vec;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==nullptr)return false;
        vec1=sumtarget(root);
        for(int i=0;i<vec1.size();i++)  
        {
            if(targetSum==vec1[i])
            return true;
        }
        return false;
    }
};

自己做的时候吸取了上一题的“回溯”思想。

代码随想录代码

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

一个不同的想考方法,用总的值减去每个节点的值,递归的题目感觉不用深入思考每个细节的执行流程,只要知道每个模块的功能再去调用就好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值