Complete the code to declare the TreeNode class with left and right children.
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; left = [1]; right = null; } }
When initializing a new node, its left and right children should start as null because they don't point to any nodes yet.
Complete the code to add the root node's value to the result list when visiting a new level.
if (level == result.Count) { result.[1](node.val); }
We use Add to append the node's value to the result list when we first visit a new level.
Fix the error in the recursive call to traverse the left subtree first.
if (node.left != null) { LeftView(node.[1], level + 1, result); }
To get the left side view, we must traverse the left child first, so we use node.left.
Fill both blanks to complete the recursive calls for left and right children in order.
if (node.[1] != null) { LeftView(node.[1], level + 1, result); } if (node.[2] != null) { LeftView(node.[2], level + 1, result); }
We first traverse the left child, then the right child to capture the left side view correctly.
Fill all three blanks to complete the method signature and initial call for left side view.
public IList<int> LeftSideView([1] root) { var result = new List<int>(); if (root != null) { LeftView(root, [2], result); } return result; } private void LeftView(TreeNode node, int [3], IList<int> result) { // recursive logic here }
The method takes a TreeNode as root, starts level at 0, and uses level as the parameter name in recursion.
