leetcode习题集——102. 二叉树的层次遍历

题目

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],
leetcode习题集——102. 二叉树的层次遍历_第1张图片

算法

public class P102 {
    /**
     * 层序遍历
     * 递归
     */
    public List<List<Integer>> levelOrder(TreeNode Node) {
        List<List<Integer>> rList = new ArrayList<>();
        if (Node == null) {
            return rList;
        }
        //求树的深度
        int depth = depth(Node);
        //根据层次进行遍历
        for (int i = 1; i <= depth; i++) {
            List<Integer> leveli = new ArrayList<>();
            levelOrder(Node, i,leveli);
            rList.add(leveli);
        }
        return rList;
    }

    private void levelOrder(TreeNode Node, int level,List<Integer> leveli) {
        if (Node == null || level < 1) {
            return;
        }

        if (level == 1) {
            leveli.add(Node.val);
            return;
        }
        // 左子树
        levelOrder(Node.left, level - 1,leveli);
        // 右子树
        levelOrder(Node.right, level - 1,leveli);
    }
    /**
     * 求树高
     * 递归
     */
    public int depth(TreeNode Node) {
        if (Node == null) {
            return 0;
        }
        int l = depth(Node.left);
        int r = depth(Node.right);
        if (l > r) {
            return l + 1;
        } else {
            return r + 1;
        }
    }
}

思路:递归求解,先求树高,再一层一层的遍历打印

算法2

public class P102_2 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> rList = new ArrayList<>();
        if (root == null) {
            return rList;
        }
        TreeLevel binaryNode;
        Queue<TreeLevel> queue = new LinkedList<>();
        //根节点入队
        queue.add(new TreeLevel(root,0));

        while (queue.size() != 0) {
            List<Integer> leveli ;
            //根节点出队
            binaryNode = queue.poll();
            //访问
            try{
                leveli = rList.get(binaryNode.level);
            }catch (IndexOutOfBoundsException e){
                leveli = new ArrayList<>();
                rList.add(leveli);
            }
            leveli.add(binaryNode.node.val);
            if (binaryNode.node.left != null) {
                //左子树入队
                queue.offer(new TreeLevel(binaryNode.node.left,binaryNode.level+1));
            }
            if (binaryNode.node.right != null) {
                //右子树入队
                queue.offer(new TreeLevel(binaryNode.node.right,binaryNode.level+1));
            }
        }
        return rList;
    }
    private class TreeLevel{
        public int level;
        public TreeNode node;
        public TreeLevel(TreeNode node,int level){
            this.level = level;
            this.node = node;
        }
        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public TreeNode getNode() {
            return node;
        }

        public void setNode(TreeNode node) {
            this.node = node;
        }
    }
}

思路:入队出队实现层次遍历

算法3

public class P102_4 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if(root==null){
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int length = 1;
        while(!queue.isEmpty()){
            List<Integer> subList = new ArrayList<>();
            for(int i=0;i<length;i++){
                TreeNode t = queue.poll();
                if(t==null){
                    continue;
                }
                subList.add(t.val);
                if(t.left!=null){
                    queue.add(t.left);
                }
                if(t.right!=null){
                    queue.add(t.right);
                }
            }
            list.add(subList);
            length=queue.size();
        }
        return list;
    }
}

思想:队列实现层次遍历,层次输出,这里用length记录了每一层的个数

算法4

public class P102_3 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) return new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        dfs(root, 0, result);
        return result;
    }
    //思想:前序遍历,层次输出
    public void dfs(TreeNode node, int level, List<List<Integer>> result) {
        if (node == null) return;
        if (result.size() < level + 1) result.add(new ArrayList<>());
        result.get(level).add(node.val);
        dfs(node.left, level + 1, result);
        dfs(node.right, level + 1, result);
    }
}

思想:前序遍历,层次输出

你可能感兴趣的:(算法,递归分治,java,二叉树)