-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1038.java
More file actions
41 lines (34 loc) · 1.08 KB
/
1038.java
File metadata and controls
41 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int rinorder( TreeNode root , int sumSoFar ){
if( root == null) {
return sumSoFar;
}
//update for right subtree
sumSoFar = rinorder( root.right, sumSoFar);
//update for this node
sumSoFar += root.val;
//store
root.val = sumSoFar ;
//update for left subtree
sumSoFar = rinorder( root.left, sumSoFar);
//return
return sumSoFar;
}
public TreeNode bstToGst(TreeNode root) {
rinorder(root , 0 );
return root;
}
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________