`{"id":"3d738021-0136-420f-a566-fd7bc97cea08","name":"Maximum Path Sum Of Binary Tree","description":"1. You are given a partially written function to solve.\r\n\r\n2. You are required to complete the body of maxPathSum function. The function is expected to return Integer value depending upon node-to-node paths where path sum equals targetSum.\r\n\r\n3. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.\r\n\r\n4. The path sum of a path is the sum of the node's values in the path.\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^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nclass TreeNode\n{\npublic:\n int val = 0;\n TreeNode* left = nullptr;\n TreeNode* right = nullptr;\n\n TreeNode(int val)\n {\n this->val = val;\n }\n};\n\n int maxPathSum(TreeNode *root) {\n // Write your code here\n return 0;\n }\n\n\n// input_Section=================================================\n\nTreeNode* createTree(vector<int>& arr, vector<int>& IDX)\n{\n if (IDX > arr.size() || arr[IDX] == -1)\n {\n IDX++;\n return nullptr;\n }\n TreeNode* node = new TreeNode(arr[IDX++]);\n node->left = createTree(arr, IDX);\n node->right = createTree(arr, IDX);\n\n return node;\n}\n\nvoid solve()\n{\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n int tar;\n cin >> tar;\n vector<int> IDX(1, 0);\n TreeNode* root = createTree(arr, IDX);\n cout<<maxPathSum(root);\n}\n\nint main()\n{\n solve();\n return 0;\n}"},"java":{"code":"import java.util.Scanner;\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 maxPathSum(TreeNode root) {\r\n return 0;\r\n }\r\n\r\n // input_Section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int;\r\n TreeNode root = createTree(arr, IDX);\r\n System.out.println(maxPathSum(root));\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n1\r\n2\r\n-1\r\n-1\r\n3\r\n-1\r\n-1","sampleOutput":"6\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":"a92eb59d-f622-48f7-bc90-76a9ddde233b","name":"Maximum Path Sum Of Binary Tree MCQ","slug":"maximum-path-sum-of-binary-tree-mcq","type":0},{"id":"4a39c447-c4b3-45fe-b29a-712d7d3aa495","name":"Maximum Path Sum Of Binary Tree","slug":"maximum-path-sum-of-binary-tree","type":1}],"next":null,"prev":null}}`

# Maximum Path Sum Of Binary Tree

1. You are given a partially written function to solve. 2. You are required to complete the body of maxPathSum function. The function is expected to return Integer value depending upon node-to-node paths where path sum equals targetSum. 3. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. 4. The path sum of a path is the sum of the node's values in the path.

`{"id":"3d738021-0136-420f-a566-fd7bc97cea08","name":"Maximum Path Sum Of Binary Tree","description":"1. You are given a partially written function to solve.\r\n\r\n2. You are required to complete the body of maxPathSum function. The function is expected to return Integer value depending upon node-to-node paths where path sum equals targetSum.\r\n\r\n3. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.\r\n\r\n4. The path sum of a path is the sum of the node's values in the path.\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^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nclass TreeNode\n{\npublic:\n int val = 0;\n TreeNode* left = nullptr;\n TreeNode* right = nullptr;\n\n TreeNode(int val)\n {\n this->val = val;\n }\n};\n\n int maxPathSum(TreeNode *root) {\n // Write your code here\n return 0;\n }\n\n\n// input_Section=================================================\n\nTreeNode* createTree(vector<int>& arr, vector<int>& IDX)\n{\n if (IDX > arr.size() || arr[IDX] == -1)\n {\n IDX++;\n return nullptr;\n }\n TreeNode* node = new TreeNode(arr[IDX++]);\n node->left = createTree(arr, IDX);\n node->right = createTree(arr, IDX);\n\n return node;\n}\n\nvoid solve()\n{\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n int tar;\n cin >> tar;\n vector<int> IDX(1, 0);\n TreeNode* root = createTree(arr, IDX);\n cout<<maxPathSum(root);\n}\n\nint main()\n{\n solve();\n return 0;\n}"},"java":{"code":"import java.util.Scanner;\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 maxPathSum(TreeNode root) {\r\n return 0;\r\n }\r\n\r\n // input_Section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int;\r\n TreeNode root = createTree(arr, IDX);\r\n System.out.println(maxPathSum(root));\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n1\r\n2\r\n-1\r\n-1\r\n3\r\n-1\r\n-1","sampleOutput":"6\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":"a92eb59d-f622-48f7-bc90-76a9ddde233b","name":"Maximum Path Sum Of Binary Tree MCQ","slug":"maximum-path-sum-of-binary-tree-mcq","type":0},{"id":"4a39c447-c4b3-45fe-b29a-712d7d3aa495","name":"Maximum Path Sum Of Binary Tree","slug":"maximum-path-sum-of-binary-tree","type":1}],"next":null,"prev":null}}` Editor

# Maximum Path Sum Of Binary Tree

medium

1. You are given a partially written function to solve. 2. You are required to complete the body of maxPathSum function. The function is expected to return Integer value depending upon node-to-node paths where path sum equals targetSum. 3. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. 4. The path sum of a path is the sum of the node's values in the path.

## Constraints

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

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 1 2 -1 -1 3 -1 -1```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6 ```

Discussions

Show Discussion

Related Resources 