Skip to content

Commit 8eab13a

Browse files
committed
Added a tree solution
1 parent 7c83ff4 commit 8eab13a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
12+
int t = Integer.MAX_VALUE;
13+
int prev = Integer.MAX_VALUE;
14+
15+
public int minDiffInBST(TreeNode root) {
16+
inorder(root);
17+
18+
return t;
19+
}
20+
21+
public void inorder(TreeNode root) {
22+
if (root == null) {
23+
return;
24+
}
25+
26+
inorder(root.left);
27+
if (prev != Integer.MAX_VALUE) {
28+
t = Math.min(t, Math.abs(root.val - prev));
29+
}
30+
31+
prev = root.val;
32+
inorder(root.right);
33+
}
34+
}

0 commit comments

Comments
 (0)