# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:defmaxDepth(self,root:TreeNode)->int:ifrootisNone:return0return1+max(self.maxDepth(root.left),self.maxDepth(root.right))
1 2 3 4 5 6 7 8 91011121314151617
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicintmaxDepth(TreeNoderoot){if(root==null){return0;}return1+Math.max(maxDepth(root.left),maxDepth(root.right));}}
1 2 3 4 5 6 7 8 9101112131415161718
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classSolution{public:intmaxDepth(TreeNode*root){if(!root){return0;}return1+max(maxDepth(root->left),maxDepth(root->right));}};
1 2 3 4 5 6 7 8 9101112131415161718
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcmaxDepth(root*TreeNode)int{ifroot==nil{return0}l,r:=maxDepth(root.Left),maxDepth(root.Right)ifl>r{return1+l}return1+r}
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number} */varmaxDepth=function(root){if(!root){return0;}return1+Math.max(maxDepth(root.left),maxDepth(root.right));};
1 2 3 4 5 6 7 8 91011121314151617
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */publicclassSolution{publicintMaxDepth(TreeNoderoot){if(root==null){return0;}return1+Math.Max(MaxDepth(root.left),MaxDepth(root.right));}}
1 2 3 4 5 6 7 8 91011121314151617181920
/* public class TreeNode {* public var val: Int* public var left: TreeNode?* public var right: TreeNode?* public init(_ val: Int) {* self.val = val* self.left = nil* self.right = nil* }* }*/classSolution{funcmaxDepth(_root:TreeNode?)->Int{guardletroot=rootelse{return0}return1+max(maxDepth(root.left),maxDepth(root.right))}}