标签:字符 remove lock 算法 组合 ext dfs offer size
使用场景:常见于各种组合,树遍历,前序遍历、中序遍历、后续遍历
private void dfs(int[] arr, int target,int begin, Deque<Integer> path, Set<List<Integer>> res) {
if ( path.size()== target) {
res.add(new ArrayList<>(path));
return;
}else if(begin>arr.length){
return;
}
for (int i = begin; i < arr.length; i++) {
path.add(arr[i]);
dfs(arr,target,i+1,path,res);
path.removeLast();
}
}
常见于树遍历,树层级,节点next指针等场景
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> levelOrder = new LinkedList<List<Integer>>();
if (root == null) {
return levelOrder;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<Integer>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
TreeNode left = node.left, right = node.right;
if (left != null) {
queue.offer(left);
}
if (right != null) {
queue.offer(right);
}
}
levelOrder.add(0, level);
}
return levelOrder;
}
常见于求最长连续字符、累计值最大等场景,
回文字符串
参见快慢指针
标签:字符 remove lock 算法 组合 ext dfs offer size
原文地址:https://www.cnblogs.com/jason0529/p/13855486.html