538. The binary search tree into a tree cumulative

convert-bst-to-greater-tree

Title Description
Title Description

Code

public class Solution {
	private int sum = 0;
	
	public TreeNode convertBST(TreeNode root){
		if(root!=null){
			//下面的遍历过程其实是把此方法返回值当void处理的
			convertBST(root.right);
			sum += root.val;
			root.val = sum;
			convertBST(root.left);
		}
		return root;
	}
}

Performance
Performance

Published 75 original articles · won praise 0 · Views 1502

Guess you like

Origin blog.csdn.net/qq_34087914/article/details/104138369
Recommended