[LintCode] Maximum Depth of Binary Tree
public int maxDepth(TreeNode root) { // write your code here if(root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right))+1; }
public int maxDepth(TreeNode root) { // write your code here if(root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right))+1; }
public int numIslands(boolean[][] grid) { // Write your code here if(grid == null) return 0; int res = 0; for(int i = 0 ; i < grid.length; i++) { for(int j = 0; j < grid[0].length; j++) { if(grid[i][j]) res++; dfs(grid,i,j); } } return res; } public void dfs(boolean[][] m, int i, int j) { […]
/** * @param nums: A list of integers. * @return: A list of permutations. */ public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(nums == null || nums.size() == 0) return res; boolean[] visited = new boolean[nums.size()]; ArrayList<Integer> tmp = new ArrayList<Integer>(); dfs(res,visited, tmp, nums); return res; } […]
public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> S) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(S == null || S.size() == 0) return res; Collections.sort(S); ArrayList<Integer> tmp = new ArrayList<Integer>(); res.add(tmp); dfs(res, S, tmp, 0); return res; } public void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> S, ArrayList<Integer> tmp, int pos) { for(int i = pos; i < […]