给你一棵以 root 为根的 二叉树  ,请你返回 任意  二叉搜索子树的最大键值和。
二叉搜索树的定义如下:
任意节点的左子树中的键值都 小于  此节点的键值。 
任意节点的右子树中的键值都 大于  此节点的键值。 
任意节点的左子树和右子树都是二叉搜索树。 
 
示例 1: 
; 
maxValue 设置为 right.maxValue 与 root.val 的最大值(原因同第 2 条); 
sumValue 设置为 left.sumValue 与 right.sumValue 之和。 
 
同时,用 sumValue 更新题目答案,取所有二叉搜索树中的最大值。
值得一提的是,本题需要把所有节点都遍历一次,因为每个子树都有成为二叉搜索树的可能。即便我们可以根据某个节点的左子树不是二叉搜索树就可以确定该子树不是二叉搜索树,我们也需要去遍历该结点的右子树。因为其右子树及其子树仍有成为二叉搜索树的可能。
代码 
[sol1-C++] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class  Solution  {public :    static  constexpr  int  inf = 0x3f3f3f3f ;     int  res;     struct  SubTree  {         bool  isBST;         int  minValue;         int  maxValue;         int  sumValue;         SubTree (bool  isBST, int  minValue, int  maxValue, int  sumValue) : isBST (isBST), minValue (minValue), maxValue (maxValue), sumValue (sumValue) {}     };     SubTree dfs (TreeNode* root)   {         if  (root == nullptr ) {             return  SubTree (true , inf, -inf, 0 );         }         auto  left = dfs (root->left);         auto  right = dfs (root->right);         if  (left.isBST && right.isBST &&                 root->val > left.maxValue &&                  root->val < right.minValue) {             int  sum = root->val + left.sumValue + right.sumValue;             res = max (res, sum);             return  SubTree (true , min (left.minValue, root->val),                             max (root->val, right.maxValue), sum);         } else  {             return  SubTree (false , 0 , 0 , 0 );         }     }     int  maxSumBST (TreeNode* root)           res = 0 ;         dfs (root);         return  res;     } }; 
[sol1-Java] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class  Solution  {    static  final  int  INF  =  0x3f3f3f3f ;     int  res;     class  SubTree  {         boolean  isBST;         int  minValue;         int  maxValue;         int  sumValue;         SubTree(boolean  isBST, int  minValue, int  maxValue, int  sumValue) {             this .isBST = isBST;             this .minValue = minValue;             this .maxValue = maxValue;             this .sumValue = sumValue;         }     }     public  int  maxSumBST (TreeNode root)  {         res = 0 ;         dfs(root);         return  res;     }     public  SubTree dfs (TreeNode root)  {         if  (root == null ) {             return  new  SubTree (true , INF, -INF, 0 );         }         SubTree  left  =  dfs(root.left);         SubTree  right  =  dfs(root.right);         if  (left.isBST && right.isBST && root.val > left.maxValue && root.val < right.minValue) {             int  sum  =  root.val + left.sumValue + right.sumValue;             res = Math.max(res, sum);             return  new  SubTree (true , Math.min(left.minValue, root.val), Math.max(root.val, right.maxValue), sum);         } else  {             return  new  SubTree (false , 0 , 0 , 0 );         }     } } 
[sol1-Python3] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class  SubTree :    def  __init__ (self, is_bst, min_value, max_value, sum_value ):         self.is_bst = is_bst         self.min_value = min_value         self.max_value = max_value         self.sum_value = sum_value class  Solution :    INF = 0x3f3f3f3f      def  maxSumBST (self, root: Optional [TreeNode] ) -> int :         self.res = 0          self.dfs(root)         return  self.res     def  dfs (self, root ):         if  root is  None :             return  SubTree(True , self.INF, -self.INF, 0 )         left = self.dfs(root.left)         right = self.dfs(root.right)         if  left.is_bst and  right.is_bst and  root.val > left.max_value and  root.val < right.min_value:             sum  = root.val + left.sum_value + right.sum_value             self.res = max (self.res, sum )             return  SubTree(True , min (left.min_value, root.val), max (root.val, right.max_value), sum )         else :             return  SubTree(False , 0 , 0 , 0 ) 
[sol1-Go] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 const  INF = 0x3f3f3f3f type  SubTree struct  {    IsBST bool      MinVal int      MaxVal int      SumVal int  } var  res int func  maxSumBST (root *TreeNode) int  {    res = 0      dfs(root)     return  res } func  max (a int , b int ) int  {    if  a > b {         return  a     }     return  b } func  min (a int , b int ) int  {    if  a < b {         return  a     }     return  b } func  dfs (root *TreeNode)     if  root == nil  {         return  &SubTree{true , INF, -INF, 0 }     }     left := dfs(root.Left)     right := dfs(root.Right)     if  left.IsBST && right.IsBST && root.Val > left.MaxVal && root.Val < right.MinVal {         sum := root.Val + left.SumVal + right.SumVal         res = max(res, sum)         return  &SubTree{true , min(left.MinVal, root.Val), max(root.Val, right.MaxVal), sum}     } else  {         return  &SubTree{false , 0 , 0 , 0 }     } } 
[sol1-C#] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public  class  Solution  {    const  int  INF = 0x3f3f3f3f ;     int  res;     public  class  SubTree  {         public  bool  isBST;         public  int  minValue;         public  int  maxValue;         public  int  sumValue;         public  SubTree (bool  isBST, int  minValue, int  maxValue, int  sumValue             this .isBST = isBST;             this .minValue = minValue;             this .maxValue = maxValue;             this .sumValue = sumValue;         }     }     public  int  MaxSumBST (TreeNode root )         res = 0 ;         DFS(root);         return  res;     }     public  SubTree DFS (TreeNode root )         if  (root == null ) {             return  new  SubTree(true , INF, -INF, 0 );         }         SubTree left = DFS(root.left);         SubTree right = DFS(root.right);         if  (left.isBST && right.isBST && root.val > left.maxValue && root.val < right.minValue) {             int  sum = root.val + left.sumValue + right.sumValue;             res = Math.Max(res, sum);             return  new  SubTree(true , Math.Min(left.minValue, root.val), Math.Max(root.val, right.maxValue), sum);         } else  {             return  new  SubTree(false , 0 , 0 , 0 );         }     } } 
[sol1-C] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #define  MAX(a, b) ((a) > (b) ? (a) : (b)) #define  MIN(a, b) ((a) < (b) ? (a) : (b)) const  int  INF = 0x3f3f3f3f ;typedef  struct  SubTree  {    bool  isBST;     int  minValue;     int  maxValue;     int  sumValue; } SubTree; SubTree *createSubTree (bool  isBST, int  minValue,int  maxValue, int  sumValue)  {     SubTree *obj = (SubTree *)malloc (sizeof (SubTree));     obj->isBST = isBST;     obj->minValue = minValue;     obj->maxValue = maxValue;     obj->sumValue = sumValue;     return  obj; } SubTree* dfs (struct  TreeNode* root, int  *res)  {     if  (root == NULL ) {         return  createSubTree(true , INF, -INF, 0 );     }     SubTree *left = dfs(root->left, res);     SubTree *right = dfs(root->right, res);     SubTree *ret = NULL ;     if  (left->isBST && right->isBST &&           root->val > left->maxValue &&           root->val < right->minValue) {         int  sum = root->val + left->sumValue + right->sumValue;         *res = MAX(*res, sum);         ret = createSubTree(true , MIN(left->minValue, root->val), \                             MAX(root->val, right->maxValue), sum);     } else  {         ret = createSubTree(false , 0 , 0 , 0 );     }     free (left);     free (right);     return  ret; } int  maxSumBST (struct  TreeNode* root) {    int  res = 0 ;     SubTree *obj = dfs(root, &res);     free (obj);     return  res; } 
[sol1-JavaScript] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 const  INF  = 0x3f3f3f3f ;var  maxSumBST = function (root ) {    const  dfs  = (root ) => {         if  (!root) {             return  new  SubTree (true , INF , -INF , 0 );         }         let  left = dfs (root.left );         let  right = dfs (root.right );         if  (left.isBST  && right.isBST  && root.val  > left.maxValue  && root.val  < right.minValue ) {             const  sum = root.val  + left.sumValue  + right.sumValue ;             res = Math .max (res, sum);             return  new  SubTree (true , Math .min (left.minValue , root.val ), Math .max (root.val , right.maxValue ), sum);         } else  {             return  new  SubTree (false , 0 , 0 , 0 );         }     }     let  res = 0 ;     dfs (root);     return  res; }; class  SubTree  {    constructor (isBST, minValue, maxValue, sumValue ) {         this .isBST  = isBST;         this .minValue  = minValue;         this .maxValue  = maxValue;         this .sumValue  = sumValue;     } } 
复杂度分析