Topic describes

The binary tree is printed from top to bottom layer, with nodes of the same layer printed from left to right, each layer printed to a row.

Such as:

Given a binary tree: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
Copy the code

Returns the result of its hierarchical traversal:

[[3], [9,20], [15,7]Copy the code

Tip:

  • The total number of nodes <= 1000

solution

Python3

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if root is None:
            return []
        q = deque()
        res = []
        q.append(root)
        while q:
            size = len(q)
            t = []
            for _ in range(size):
                node = q.popleft()
                t.append(node.val)
                if node.left is not None:
                    q.append(node.left)
                if node.right is not None:
                    q.append(node.right)
            res.append(t)
        return res
Copy the code

Java

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) return Collections.emptyList(); Deque<TreeNode> q = new ArrayDeque<>(); List<List<Integer>> res = new ArrayList<>(); q.offer(root); while (! q.isEmpty()) { int size = q.size(); List<Integer> t = new ArrayList<>(); while (size-- > 0) { TreeNode node = q.poll(); t.add(node.val); if (node.left ! = null) q.offer(node.left); if (node.right ! = null) q.offer(node.right); } res.add(t); } return res; }}Copy the code

JavaScript

/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[][]} */ var levelOrder = function (root) { if (! root) return []; let queue = [root]; let res = []; let depth = 0; while (queue.length) { let len = queue.length; for (let i = 0; i < len; i++) { let node = queue.shift(); if (! node) continue; if (! res[depth]) res[depth] = []; res[depth].push(node.val); queue.push(node.left, node.right); } depth++; } return res; };Copy the code

C++

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if (root == NULL) return ans;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            int n = q.size();
            vector<int> v;
            for (int i = 0; i < n; ++i) {
                TreeNode* node = q.front();
                q.pop();
                v.emplace_back(node->val);
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            ans.emplace_back(v);
        }
        return ans;
    }
};
Copy the code