Question
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.
My C# Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root) {
if (root == null) return 0;
var leftDepth = MaxDepth(root.left) + 1;
var rightDepth = MaxDepth(root.right) + 1;
return (leftDepth > rightDepth) ? leftDepth : rightDepth;
}
}