1. 程式人生 > >Leetcode-5050 Binary Search Tree to Greater Sum Tree(從二叉搜索樹到更大和樹)

Leetcode-5050 Binary Search Tree to Greater Sum Tree(從二叉搜索樹到更大和樹)

基本 greate reat ear ret col pub div class

第二題我完全沒看懂他想表達什麽,不過通過例子去理解,基本上就是右中左這種順序把結點的值換一下吧

 1 class Solution
 2 {
 3     public:
 4         int rnt = 0;
 5         TreeNode* bstToGst(TreeNode* root)
 6         {
 7             if(!root)
 8                 return NULL;
 9             bstToGst(root->right);
10             int tmp = root->val;
11 root->val += rnt; 12 rnt += tmp; 13 bstToGst(root->left); 14 return root; 15 } 16 };

Leetcode-5050 Binary Search Tree to Greater Sum Tree(從二叉搜索樹到更大和樹)