二叉树的层序遍历 (Binary Tree Level Order Traversal)

 

思路:

// @Title: 二叉树的层序遍历 (Binary Tree Level Order Traversal)
// @Author: qisiii
// @Date: 2022-03-02 21:42:37
// @Runtime: 3 ms
// @Memory: 41.6 MB
// @comment: 
// @flag: 
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
   public static List<List<Integer>> levelOrder(TreeNode root) {
        LinkedBlockingQueue queue = new LinkedBlockingQueue();
        List<List<Integer>> result = new ArrayList<>();
        if (root==null){
            return result;
        }
        queue.offer(root);
        doTree(queue, result);
        return result;
    }

    public static void doTree(Queue<TreeNode> queue, List list) {
        if (queue==null||queue.isEmpty()){
            return;
        }
        List<Integer> objects = new ArrayList<>();
        Queue<TreeNode> temp = new LinkedBlockingQueue();;
        while (!queue.isEmpty()) {
            TreeNode poll = queue.remove();
            if (poll==null){
                continue;
            }
            objects.add(poll.val);
            if (poll.left!=null){
                temp.offer(poll.left);
            }
            if (poll.right!=null){
                temp.offer(poll.right);
            }
        }
        if (!objects.isEmpty()) {
            list.add(objects);
        }
        doTree(temp, list);
    }

}

+++ title = “二叉树的层序遍历 (Binary Tree Level Order Traversal)” draft = false +++

思路:bfs,队列层级遍历

// @Title: 二叉树的层序遍历 (Binary Tree Level Order Traversal)
// @Author: qisiii
// @Date: 2024-01-14 19:25:34
// @Runtime: 1 ms
// @Memory: 43.7 MB
// @comment: bfs,队列层级遍历
// @flag: WHITE
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
               List<List<Integer>> result= new ArrayList();
        if(root==null){
            return result;
        }
                result.add(new ArrayList(Arrays.asList(root.val)));
       List<TreeNode> rootList=new ArrayList();
        rootList.add(root);
       bfs(rootList,result);
       return result;
    }
    void bfs(List<TreeNode> queue,List<List<Integer>> result){
        if (queue == null||queue.size()<=0){
            return;
        }
        List<TreeNode> temp=new ArrayList();
        List<Integer> level=new ArrayList();
        for(int i=0;i<queue.size();i++){
            TreeNode node=queue.get(i);
            if(node.left!=null){
                temp.add(node.left);
                level.add(node.left.val);
            }
            if(node.right!=null){
                temp.add(node.right);
                level.add(node.right.val);
            }
        }
        if(level.size()>0){
        result.add(level);
        }
        bfs(temp,result);
    }
}

思路:

// @Title: 二叉树的层序遍历 (Binary Tree Level Order Traversal)
// @Author: qisiii
// @Date: 2024-09-14 23:21:15
// @Runtime: 1 ms
// @Memory: 43.9 MB
// @comment: 
// @flag: 
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> temp = new ArrayList<>();
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                temp.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            result.add(temp);
        }
        return result;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18