103_二叉树的锯齿形层序遍历
小于 1 分钟
103_二叉树的锯齿形层序遍历中等
class Solution {
List<List<Integer>>list=new ArrayList<>();
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if(root==null){
return list;
}
fun(root,0);
return list;
}
public void fun(TreeNode node,int level){
if(level==list.size()){
List<Integer>list1=new ArrayList<>();
list1.add(node.val);
list.add(list1);
}else {
if(level%2==0){
list.get(level).add(node.val);
}else {
list.get(level).add(0,node.val);
}
}
if(node.left!=null){
fun(node.left,level+1);
}
if(node.right!=null){
fun(node.right,level+1);
}
}
}
Powered by Waline v2.15.5