题解 | #二叉树中和为某一值的路径(一)#

二叉树中和为某一值的路径(一)

https://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c

/**
 * struct TreeNode {
 *  int val;
 *  struct TreeNode *left;
 *  struct TreeNode *right;
 * };
 */

#include <limits>
class Solution {
  public:
    /**
     *     vector<int> tree;
    vector<int> preorderTraversal(TreeNode* root) {
        // write code here
       if(root==nullptr){
        return tree;
       }
       tree.push_back(root->val);
       preorderTraversal(root->left);
        preorderTraversal(root->right);

        return tree;
    }
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
     int num = 0;
    bool hasPathSum(TreeNode* root, int sum) {
        // write code here
        if(!root){
            return false;
        } 
        if(root&&root->val == sum && root->right == nullptr && root->left == nullptr){
            return true;
        }
        return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
    
    }
};

全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务