538. Convert BST to Greater Tree

来源:互联网 发布:python str.format 编辑:程序博客网 时间:2024/06/07 20:02

题目:

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:              5            /   \           2     13Output: The root of a Greater Tree like this:             18            /   \          20     13
思路:

对于树结构的题目,本题使用递归的方法,步骤如下:

step1:中序遍历,存储得升序的数组

step2;中序遍历,赋值

代码:

/** * 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:    TreeNode* convertBST(TreeNode* root) {        vector<int> res;        InOrder(root,res);        for(int i =0;i<res.size();i++)        {            for(int j=i+1;j<res.size();j++)            {                res[i] +=res[j];             }        }        #sort(res.begin(),res.end());        int i= 0;        InOrder_change(root,res,i);        return root;            }private:      void InOrder(TreeNode* T,vector<int>& res){        if(T != NULL){            //访问左子结点            InOrder(T->left,res);            //访问根节点            res.push_back(T->val);          //访问右子结点            InOrder(T->right,res);        }    }      void InOrder_change(TreeNode* T, vector<int>& res,int& i){    if(T != NULL){            //访问左子结点                InOrder_change(T->left,res,i);            //访问根节点         T->val = res[i];        i++;        //访问右子结点        InOrder_change(T->right,res,i);        }    } };


原创粉丝点击