Last active
March 2, 2021 17:11
-
-
Save feruxmax/5edb3dfdbdd0ee5d09e6e76ca1a143a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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