LINTCODE——Convert BST to Greater Tree

来源:互联网 发布:阈值分割算法 matlab 编辑:程序博客网 时间:2024/06/03 20:44

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;     }};
原创粉丝点击