【leetcode】102. 二叉树的层序遍历

水题 切了

package pro102;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: qiqu
 */
public class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    public List> levelOrder(TreeNode root) {
        Queue q = new LinkedList<>();
        List> result = new LinkedList<>();
        if(root == null){
            return result;
        }

        List list1 = new LinkedList<>();
        list1.add(root.val);
        result.add(list1);
        int cnt=0;
        if(root.left != null){
            cnt++;
            q.offer(root.left);
        }
        if(root.right != null){
            cnt++;
            q.offer(root.right);
        }

        while(!q.isEmpty()){
            List list = new LinkedList<>();
            int tmp = 0;
            for(int i=0;i

你可能感兴趣的:(leetcode)