{"id":"75a24b93-047d-4039-be72-86aa9269130a","name":"Path Sum Equal To Given Value","description":"1. Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.\r\n2. The path does not need to start or end at the root or a leaf,path can start from any where but it must go downwards\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you. \r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n\r\n","sampleCode":{"cpp":{"code":"// #include <iostream>\r\n// #include <vector>\r\n// #include <queue>\r\n// #include <stringstream>\r\n#include <bits/stdc++.h>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode* left = nullptr;\r\n TreeNode* right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nint pathSum(TreeNode* root, int sum)\r\n{\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode* node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nint idx = 0;\r\nTreeNode* deserialize(vector<string>& arr)\r\n{\r\n if (idx >= arr.size() || arr[idx].compare(\"null\") == 0)\r\n {\r\n idx++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode* node = new TreeNode(stoi(arr[idx++]));\r\n node->left = deserialize(arr);\r\n node->right = deserialize(arr);\r\n\r\n return node;\r\n}\r\n\r\nTreeNode* deserialize(string s)\r\n{\r\n stringstream ss(s);\r\n string word;\r\n vector<string> arr;\r\n while (ss >> word)\r\n {\r\n arr.push_back(word);\r\n }\r\n return deserialize(arr);\r\n}\r\n\r\nvoid solve()\r\n{\r\n string s;\r\n cin >> s;\r\n TreeNode* root = deserialize(s);\r\n int tar;\r\n cin >> tar;\r\n\r\n cout << pathSum(root, tar) << endl;\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static int pathSum(TreeNode root, int K) {\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static int idx = 0;\r\n\r\n private static TreeNode deserialize(String[] arr) {\r\n if (idx >= arr.length || arr[idx].equals(\"null\")) {\r\n idx++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(Integer.parseInt(arr[idx++]));\r\n node.left = deserialize(arr);\r\n node.right = deserialize(arr);\r\n\r\n return node;\r\n }\r\n\r\n public static TreeNode deserialize(String str) {\r\n String[] arr = str.split(\" \");\r\n return deserialize(arr);\r\n }\r\n\r\n public static void solve() {\r\n String str = scn.nextLine();\r\n TreeNode root = deserialize(str);\r\n int tar = scn.nextInt();\r\n\r\n int ans = pathSum(root, tar);\r\n System.out.println(ans);\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"10 5 3 3 null null -2 null null 2 null 1 null null -3 null 11 null null\r\n8","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"0fca7e82-3626-46cc-ba5c-06588c081d24","name":"Path Sum Equal To Given Value MCQ","slug":"path-sum-equal-to-given-value-mcq","type":0},{"id":"4f102189-02c6-4634-be42-830e2c8368df","name":"Path Sum Equal To Given Value","slug":"path-sum-equal-to-given-value","type":1}],"next":null,"prev":null}}

Path Sum Equal To Given Value

1. Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum. 2. The path does not need to start or end at the root or a leaf,path can start from any where but it must go downwards

{"id":"75a24b93-047d-4039-be72-86aa9269130a","name":"Path Sum Equal To Given Value","description":"1. Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.\r\n2. The path does not need to start or end at the root or a leaf,path can start from any where but it must go downwards\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you. \r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n\r\n","sampleCode":{"cpp":{"code":"// #include <iostream>\r\n// #include <vector>\r\n// #include <queue>\r\n// #include <stringstream>\r\n#include <bits/stdc++.h>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode* left = nullptr;\r\n TreeNode* right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nint pathSum(TreeNode* root, int sum)\r\n{\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode* node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nint idx = 0;\r\nTreeNode* deserialize(vector<string>& arr)\r\n{\r\n if (idx >= arr.size() || arr[idx].compare(\"null\") == 0)\r\n {\r\n idx++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode* node = new TreeNode(stoi(arr[idx++]));\r\n node->left = deserialize(arr);\r\n node->right = deserialize(arr);\r\n\r\n return node;\r\n}\r\n\r\nTreeNode* deserialize(string s)\r\n{\r\n stringstream ss(s);\r\n string word;\r\n vector<string> arr;\r\n while (ss >> word)\r\n {\r\n arr.push_back(word);\r\n }\r\n return deserialize(arr);\r\n}\r\n\r\nvoid solve()\r\n{\r\n string s;\r\n cin >> s;\r\n TreeNode* root = deserialize(s);\r\n int tar;\r\n cin >> tar;\r\n\r\n cout << pathSum(root, tar) << endl;\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static int pathSum(TreeNode root, int K) {\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static int idx = 0;\r\n\r\n private static TreeNode deserialize(String[] arr) {\r\n if (idx >= arr.length || arr[idx].equals(\"null\")) {\r\n idx++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(Integer.parseInt(arr[idx++]));\r\n node.left = deserialize(arr);\r\n node.right = deserialize(arr);\r\n\r\n return node;\r\n }\r\n\r\n public static TreeNode deserialize(String str) {\r\n String[] arr = str.split(\" \");\r\n return deserialize(arr);\r\n }\r\n\r\n public static void solve() {\r\n String str = scn.nextLine();\r\n TreeNode root = deserialize(str);\r\n int tar = scn.nextInt();\r\n\r\n int ans = pathSum(root, tar);\r\n System.out.println(ans);\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"10 5 3 3 null null -2 null null 2 null 1 null null -3 null 11 null null\r\n8","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"0fca7e82-3626-46cc-ba5c-06588c081d24","name":"Path Sum Equal To Given Value MCQ","slug":"path-sum-equal-to-given-value-mcq","type":0},{"id":"4f102189-02c6-4634-be42-830e2c8368df","name":"Path Sum Equal To Given Value","slug":"path-sum-equal-to-given-value","type":1}],"next":null,"prev":null}}
plane

Editor


Loading...

Path Sum Equal To Given Value

medium

1. Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum. 2. The path does not need to start or end at the root or a leaf,path can start from any where but it must go downwards

Constraints

0 <= Number of Nodes <= 10^5 -1000 <= value of Node data <= 1000

Format

Input

Input is managed for you.

Output

Output is managed for you.

Example

Sample Input

10 5 3 3 null null -2 null null 2 null 1 null null -3 null 11 null null 8

Sample Output

3

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode