Skip to content

Instantly share code, notes, and snippets.

@feruxmax
Last active March 2, 2021 17:11
Show Gist options
  • Select an option

  • Save feruxmax/5edb3dfdbdd0ee5d09e6e76ca1a143a9 to your computer and use it in GitHub Desktop.

Select an option

Save feruxmax/5edb3dfdbdd0ee5d09e6e76ca1a143a9 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root)
{
DFS(root, recursionLevel: 0);
return maxDepth;
}
private void DFS(TreeNode node, int recursionLevel)
{
if (node == null)
{
return;
}
DFS(node.left, recursionLevel + 1);
Visit(node, recursionLevel);
DFS(node.right, recursionLevel + 1);
}
private int maxDepth;
private void Visit(TreeNode node, int recursionLevel)
{
maxDepth = recursionLevel + 1 > maxDepth ? recursionLevel + 1 : maxDepth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment