|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1373 lang=cpp |
| 3 | + * |
| 4 | + * [1373] Maximum Sum BST in Binary Tree |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Hard (44.77%) |
| 10 | + * Likes: 101 |
| 11 | + * Dislikes: 15 |
| 12 | + * Total Accepted: 3.8K |
| 13 | + * Total Submissions: 8.6K |
| 14 | + * Testcase Example: '[1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]' |
| 15 | + * |
| 16 | + * Given a binary tree root, the task is to return the maximum sum of all keys |
| 17 | + * of any sub-tree which is also a Binary Search Tree (BST). |
| 18 | + * |
| 19 | + * Assume a BST is defined as follows: |
| 20 | + * |
| 21 | + * |
| 22 | + * The left subtree of a node contains only nodes with keys less than the |
| 23 | + * node's key. |
| 24 | + * The right subtree of a node contains only nodes with keys greater than the |
| 25 | + * node's key. |
| 26 | + * Both the left and right subtrees must also be binary search trees. |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * Example 1: |
| 31 | + * |
| 32 | + * |
| 33 | + * |
| 34 | + * |
| 35 | + * Input: root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6] |
| 36 | + * Output: 20 |
| 37 | + * Explanation: Maximum sum in a valid Binary search tree is obtained in root |
| 38 | + * node with key equal to 3. |
| 39 | + * |
| 40 | + * |
| 41 | + * Example 2: |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * |
| 46 | + * Input: root = [4,3,null,1,2] |
| 47 | + * Output: 2 |
| 48 | + * Explanation: Maximum sum in a valid Binary search tree is obtained in a |
| 49 | + * single root node with key equal to 2. |
| 50 | + * |
| 51 | + * |
| 52 | + * Example 3: |
| 53 | + * |
| 54 | + * |
| 55 | + * Input: root = [-4,-2,-5] |
| 56 | + * Output: 0 |
| 57 | + * Explanation: All values are negatives. Return an empty BST. |
| 58 | + * |
| 59 | + * |
| 60 | + * Example 4: |
| 61 | + * |
| 62 | + * |
| 63 | + * Input: root = [2,1,3] |
| 64 | + * Output: 6 |
| 65 | + * |
| 66 | + * |
| 67 | + * Example 5: |
| 68 | + * |
| 69 | + * |
| 70 | + * Input: root = [5,4,8,3,null,6,3] |
| 71 | + * Output: 7 |
| 72 | + * |
| 73 | + * |
| 74 | + * |
| 75 | + * Constraints: |
| 76 | + * |
| 77 | + * |
| 78 | + * Each tree has at most 40000 nodes.. |
| 79 | + * Each node's value is between [-4 * 10^4 , 4 * 10^4]. |
| 80 | + * |
| 81 | + */ |
| 82 | + |
| 83 | +// @lc code=start |
| 84 | +/** |
| 85 | + * Definition for a binary tree node. |
| 86 | + * struct TreeNode { |
| 87 | + * int val; |
| 88 | + * TreeNode *left; |
| 89 | + * TreeNode *right; |
| 90 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 91 | + * }; |
| 92 | + */ |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + #define pr pair<bool, int> |
| 96 | + int ans; |
| 97 | + pr callme(TreeNode* root){ |
| 98 | + if(root == NULL) |
| 99 | + return make_pair(true, 0); |
| 100 | + |
| 101 | + pr left = callme(root->left); |
| 102 | + pr right = callme(root->right); |
| 103 | + |
| 104 | + bool isBst = left.first |
| 105 | + && right.first |
| 106 | + && (root->left == NULL || root->left->val < root->val) |
| 107 | + && (root->right == NULL || root->val < root->right->val); |
| 108 | + |
| 109 | + if(isBst) |
| 110 | + ans = max(ans, root->val+left.second+right.second); |
| 111 | + |
| 112 | + return make_pair(isBst, root->val+left.second+right.second); |
| 113 | + } |
| 114 | + int maxSumBST(TreeNode* root) { |
| 115 | + ans = 0; |
| 116 | + callme(root); |
| 117 | + return ans; |
| 118 | + } |
| 119 | +}; |
| 120 | +// @lc code=end |
0 commit comments