题目一
解法
class Solution {
LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> list = new LinkedList<Integer>();
boolean[] bo = new boolean[nums.length];
method(nums,bo,list);
return ans;
}
public void method(int[] nums,boolean[] bo ,LinkedList<Integer> list){
if(list.size()==nums.length){
ans.add(new LinkedList(list));
return;
}
for(int i = 0;i<nums.length;i++){
if(bo[i]){
continue;
}
list.add(nums[i]);
bo[i] = true;
method(nums,bo,list);
list.removeLast();
bo[i] = false;
}
}
}
题目二
解法
class Solution {
public int maxDepth(Node root) {
if(root==null){
return 0;
}
int maxChildDepth = 0;
for(int i = 0;i<root.children.size();i++){
int childDepth = maxDepth(root.children.get(i));
maxChildDepth = Math.max(maxChildDepth, childDepth);
}
return maxChildDepth+1;
}
}
到此这篇关于剑指Offer之Java算法习题精讲排列与N叉树的文章就介绍到这了,更多相关Java N叉树内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!