-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1026.maximum-difference-between-node-and-ancestor.java
executable file
·61 lines (59 loc) · 1.78 KB
/
1026.maximum-difference-between-node-and-ancestor.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
/*
* Maximum Searches: 3
* Searches used so far:
* 1 How to navigate a binary tree
* ended up being a very useful search, because the way leetcode does binary trees is different from what i've seen.
* basically a node can only have 2 children, and the order from input goes (Root,Left,Right)
* So [1,2,3,null,2,1,2] would be
* 1
* 2 3
* [] 2 1 2
*
*
/*
* I need to think about how I am going to navigate this tree
* */
class Solution {
public int maxDiff = 0;
public int maxAncestorDiff(TreeNode root) {
if (root == null){
return maxDiff;
}
recursiveHelper(root,root.val,root.val);
return maxDiff;
}
private void recursiveHelper(TreeNode root, int min, int max){
maxDiff = Math.max(getDiff(min,max),maxDiff);
if(root.left != null){
int lmin = (Math.min(min,root.left.val));
int lmax = (Math.max(max,root.left.val));
recursiveHelper(root.left,lmin,lmax);
}
if(root.right != null){
int rmin = (Math.min(min,root.right.val));
int rmax = (Math.max(max,root.right.val));
recursiveHelper(root.right,rmin,rmax);
}
}
public int getDiff(int a,int b){
return Math.abs((a-b));
}
}
class test {
public static void main(String[] args) {
Solution sol = new Solution();
TreeNode root = new TreeNode();
}
}