public static void PrintTreeLevelByLevel(TreeNode root) { var height = TreeUtils.Height(root); for (var level = 1; level <= height; level++) { Console.Write($"Level {level}: "); PrintGivenLevel(root, level); Console.WriteLine(); } }
public static void LevelOrderTraversalIterative1(TreeNode root) { // Time Complexity: O(n^2) in worst case. // For a skewed tree, printGivenLevel() takes O(n) time // where n is the number of nodes in the skewed tree. // So time complexity of printLevelOrder() is O(n) + O(n-1) + O(n-2) + .. + O(1) which is O(n^2) var height = TreeUtils.Height(root); Console.WriteLine("Level By Level Order Traversal"); Console.WriteLine("Height: " + height); for (var level = 1; level <= height; level++) { Console.Write($"Level {level}: "); PrintGivenLevel(root, level); Console.WriteLine(); } }