LINTCODE——Convert BST to Greater Tree
思路:考的就是另外一种遍历方式,记前序、中序、后序遍历分别为:MLR、LMR、LRM,那么题目就是要求你用RML这种遍历方式遍历更新就好了;
class Solution {
private:
int s = 0;
public:
/*
* @param root: the root of binary tree
* @return: the new root
*/
TreeNode * convertBST(TreeNode * root) {
// write your code here
if(root == NULL)
return NULL;
convertBST(root -> right);
root -> val += s;
s = root -> val;
convertBST(root -> left);
return root;
}
};