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

 

思路:

// @Title: 二叉树的层序遍历 II (Binary Tree Level Order Traversal II)
// @Author: qisiii
// @Date: 2024-09-14 23:24:04
// @Runtime: 1 ms
// @Memory: 42 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>> levelOrderBottom(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(0,temp);
        }
        return result;
    }
}

思路:先bfs再遍历即可

// @Title: 二叉树的层序遍历 II (Binary Tree Level Order Traversal II)
// @Author: qisiii
// @Date: 2024-01-14 20:06:53
// @Runtime: 1 ms
// @Memory: 41.9 MB
// @comment: 先bfs再遍历即可
// @flag: GREEN
/**
 * 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>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result = new ArrayList();
        if (root == null) {
            return result;
        }
        List<TreeNode> rootList = new ArrayList();
        rootList.add(root);
        bfs(rootList, result);
        result.add(new ArrayList(Arrays.asList(root.val)));
        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);
            }
        }
        bfs(temp, result);
        if (level.size() > 0) {
            result.add(level);
        }
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18