public int maxDepth(TreeNode root) { if (root == null) { return 0; } int result = this.maxDepth(root.left); result = Math.Max(result, this.maxDepth(root.right) ); result++; // add current level return result; }
static void Main(string[] args) { TreeNode test = new TreeNode(1); test.right = new TreeNode(2); TreeNode current = test.right; current.left = new TreeNode(3); current = current.left; current.right = new TreeNode(4); Solution s = new Solution(); Console.Out.WriteLine("Solution:{0}", s.maxDepth(test)); }