题意:把二叉查找树每个节点的值都加上比它大的节点的值。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sum = 0;
    TreeNode* convertBST(TreeNode* root) {
        if(root == NULL) return NULL;
        convertBST(root -> right);
        sum += root -> val;
        root -> val = sum;
        convertBST(root -> left);
        return root;
    }
};

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-02-11
  • 2021-06-23
  • 2022-12-23
  • 2021-08-19
  • 2021-08-11
  • 2021-09-11
猜你喜欢
  • 2021-10-11
  • 2022-02-05
  • 2022-12-23
  • 2022-01-20
相关资源
相似解决方案