Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left; 6 * public TreeNode right; 7 * public TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public int MaxDepth(TreeNode root) { 12 int[] max = new int[1] { 0 }; 13 DFS(root, 1, max); 14 return max[0]; 15 } 16 17 private void DFS(TreeNode node, int cur, int[] max) 18 { 19 if (node == null) return; 20 21 max[0] = Math.Max(cur, max[0]); 22 23 DFS(node.left, cur + 1, max); 24 DFS(node.right, cur + 1, max); 25 } 26 }