c# recursive


  • 0
    M

    public class Solution {
    IList<IList<int>> result = new List<IList<int>>();

        public IList<IList<int>> LevelOrder(TreeNode root)
        {
            if (root == null) return result;
            
            TraverseRec(root, 1);
    
            return result;
        }
    
        private void TraverseRec(TreeNode node, int level)
        {
            if (result.Count < level) result.Add(new List<int>());
    
            result[level - 1].Add(node.val);
    
            if (node.left != null) TraverseRec(node.left, level + 1);
            if (node.right != null) TraverseRec(node.right, level + 1);
        }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.