0% found this document useful (0 votes)
6 views1 page

Code

Uploaded by

jintaixu33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Code

Uploaded by

jintaixu33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

9999

530.
class Solution {
int prev = Integer.MAX_VALUE;
int ans = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return ans;
}

public void inOrder(TreeNode root){


if(root.left!=null) inOrder(root.left);
ans = Math.min(ans,Math.abs(root.val-prev));
prev = root.val;
if(root.right!=null) inOrder(root.right);
}
}

200.
public class Solution {
public int numIslands(char[][] grid) {
int count = 0;

for (int i = 0; i < grid.length; i++) {


for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == '1') {
count++;
clearRestOfLand(grid, i, j);
}
}
}
return count;
}

private void clearRestOfLand(char[][] grid, int i, int j) {


if (i < 0 || j < 0 || i >= grid.length || j >= grid[i].length || grid[i][j]
== '0')
return;

grid[i][j] = '0';
clearRestOfLand(grid, i + 1, j);
clearRestOfLand(grid, i - 1, j);
clearRestOfLand(grid, i, j + 1);
clearRestOfLand(grid, i, j - 1);
return;
}
}

You might also like