• Path Sum


    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:bool flag;
    public:
        bool hasPathSum(TreeNode *root, int sum) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            flag = false;
            if(root==NULL)
            return false;
            path(root,sum,0);
            return flag;
        }
        void path(TreeNode *root,int sum, int pre)
        {
            if(root->left==NULL&&root->right==NULL)
            {
                if(pre+root->val==sum)
                flag = true;
            }
            pre = pre+root->val;
            if(root->left!=NULL)
            {
                path(root->left,sum,pre);
            }
            if(root->right!=NULL)
            {
                path(root->right,sum,pre);
            }
        }
    };
  • 相关阅读:
    js_mongobd
    window-js-mongodb安装 错误 解决
    Mac-js-mongodb 安装
    PMP笔记-01 PMP各种图比较记忆
    kata qemu
    socat
    kata agent install syslog
    kata rootfs
    defaultKataGuestSharedDir
    kata 9p
  • 原文地址:https://www.cnblogs.com/727713-chuan/p/3334127.html
Copyright © 2020-2023  润新知