LinkSearchMenuExpandDocument

104. Maximum Depth of Binary Tree

Solution Code

C#

/** * 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; * } * } */publicclassSolution{publicintMaxDepth(TreeNoderoot){if(root==null)return0;intleftTree=MaxDepth(root.left)+1;intrightTree=MaxDepth(root.right)+1;returnleftTree>=rightTree?leftTree:rightTree;}}

© 2023. All rights reserved.

close