/**
* Created by Joe on 2018/1/17.
* 538. Convert BST to Greater Tree
* https://leetcode.com/problems/convert-bst-to-greater-tree/description/
*/
public class P538 {
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
if (root != null) {
convertBST(root.right);
sum += root.val;
root.val = sum;
convertBST(root.left);
}
return root;
}
}
这道题主要是进行求和。
但是因为是BST-二叉排序树所以是有规律的
- 根节点右面都比根节点大
- 根节点左面都比根节点小
为此使用右->中->左的中序遍历。