-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTTilt.java
More file actions
28 lines (26 loc) · 778 Bytes
/
BTTilt.java
File metadata and controls
28 lines (26 loc) · 778 Bytes
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
public class BTTilt {
public static void main(String[] args) {
TreeNode root=new TreeNode(1);
root.left=new TreeNode(2);
root.right=new TreeNode(3);
BTTilt obj=new BTTilt();
int ans= obj.findtilt(root);
System.out.println(ans);
}
public int totaltilt=0;
public int ValueSum(TreeNode root){
if (root==null){
return 0;
}
int leftsum=ValueSum(root.left);
int rightsum=ValueSum(root.right);
int tilt=Math.abs(leftsum-rightsum);
totaltilt+=tilt;
return root.data+leftsum+rightsum;
}
public int findtilt(TreeNode root){
this.totaltilt=0;
this.ValueSum(root);
return this.totaltilt;
}
}