Skip to content

Latest commit

 

History

History
33 lines (30 loc) · 642 Bytes

File metadata and controls

33 lines (30 loc) · 642 Bytes

538. Convert BST to Greater Tree

recursive // 24ms

/**
 * 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:
	void travel(TreeNode* root)
	{
		if (root == NULL)  return;
		if (root->right) travel(root->right);

		sum += root->val;
		root->val = sum;
		if (root->left) travel(root->left);
	}
	int sum = 0;
public:
	TreeNode* convertBST(TreeNode* root) {
		travel(root);
		return root;
	}
};