subject
Given a binary tree, it returns the zigzag level traversal of its node values. (that is, first from left to right, then from right to left to traverse the next layer, and so on, alternating between layers).
For example:
Given a binary tree [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
Return the zigzag level traversal as follows:
[ [3], [20,9], [15,7] ]
Title Solution
This question requires z-type, that is, to know the depth. Because knowing the depth, we can judge how to print based on the depth parity.
The first phase is sequence traversal, and then the record is the layer. We are familiar with the code of sequence traversal queue.
/** * 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>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); if (root == null) { return res; } LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); int depth = 0; while (!queue.isEmpty()) { int size = queue.size(); LinkedList<Integer> currentRes = new LinkedList<>(); // The current floor is always out of the team while (size > 0) { TreeNode current = queue.poll(); TreeNode left = current.left; TreeNode right = current.right; if (left != null) { queue.add(left); } if (right != null) { queue.add(right); } // Odd layers are added from the beginning; even layers are added from the tail if (depth % 2 != 0) { currentRes.add(0, current.val); } else { currentRes.add(current.val); } size--; } // Add the result of current layer traversal to the result res.add(currentRes); depth++; } return res; } }
As before, let's think about whether there is a recursive solution
We can use the way of traversal first, traversing the nodes first, and then traversing the left and right subtrees recursively.
With a slight change, you need to take the depth information when traversing the left and right subtrees to know whether to add it to the list header or the end of the list.
The end condition of recursion is to encounter a null node.
/** * 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>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); if (root == null) { return res; } helper(res, root, 0); return res; } public void helper(List<List<Integer>> res,TreeNode root, int depth) { if (root == null) { return; } // Note that the new List indicates that the current node recursively enters a new layer if (res.size() <= depth) { res.add(new LinkedList<>()); } if (depth % 2 != 0) { res.get(depth).add(0, root.val); } else { res.get(depth).add(root.val); } helper(res, root.left, depth + 1); helper(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