subject
Given a binary tree, it returns the value of the nodes it traverses hierarchically. (that is, access all nodes layer by layer, from left to right).
For example:
Given a binary tree: [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
Return its hierarchical traversal result:
[ [3], [9,20], [15,7] ]
Title Solution
The sequence traversal taught in our data structure book is to use a queue to continuously put left and right subtrees into the queue. But this topic also requires output according to the layer. So the key question is: how to make sure it is on the same level.
We naturally think:
If all nodes of the previous layer are dequeued before joining the team, then these nodes of the dequeued are the list of the previous layer.
Since the queue is a first in, first out data structure, the list is left to right.
/** * 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) { List<List<Integer>> res = new LinkedList<>(); if (root == null) { return res; } LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> currentRes = new LinkedList<>(); // The size of the current queue is the number of nodes in the previous layer, and the queue will be out in turn while (size > 0) { TreeNode current = queue.poll(); if (current == null) { continue; } currentRes.add(current.val); // Left subtree and right subtree join the team if (current.left != null) { queue.add(current.left); } if (current.right != null) { queue.add(current.right); } size--; } res.add(currentRes); } return res; } }
Can this problem be solved by non delivery?
Recursive subproblem: traversing the current node, for the current layer, traversing the next layer of the left subtree, traversing the next layer of the right subtree
Recursion end condition: current layer, current subtree node is null
/** * 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) { List<List<Integer>> res = new LinkedList<>(); if (root == null) { return res; } levelOrderHelper(res, root, 0); return res; } /** * @param depth The depth of binary tree */ private void levelOrderHelper(List<List<Integer>> res, TreeNode root, int depth) { if (root == null) { return; } if (res.size() <= depth) { // The first node of the current layer needs a new list to store the current layer res.add(new LinkedList<>()); } // depth layer, add the current node res.get(depth).add(root.val); // Recursively traverses the next level levelOrderHelper(res, root.left, depth + 1); levelOrderHelper(res, root.right, depth + 1); } }
Popular reading
- Summary of technical articles
- [Leetcode] 101. Symmetric binary tree
- [Leetcode] 100. Same tree
- [Leetcode] 98. Verify binary search tree