namespace _102._Binary_Tree_Level_Order_Traversal; public class Solution { public IList> LevelOrder(TreeNode root) { List> output = new List>(); RecursiveTraversal(output, new List() { root }); return output; } private void RecursiveTraversal(List> output, List curLevel) { if (curLevel.Count == 0) return; List levelValues = new List(); foreach (var n in curLevel) { if (n == null) continue; levelValues.Add(n.val); } if (levelValues.Count == 0) return; output.Add(levelValues); List nextLevel = new List(); foreach (var n in curLevel) { if (n == null) continue; if (n.left != null) nextLevel.Add(n.left); if (n.right != null) nextLevel.Add(n.right); } RecursiveTraversal(output, nextLevel); } }