/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if ( root == null )
return 0;
var left = maxDepth(root.left);
var right = maxDepth(root.right);
if ( left > right )
return left + 1;
return right + 1;
};
c 解法, 执行用时: 16 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:46:45
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root) {
if ( root == NULL )
return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
if ( left > right )
return left + 1;
return right + 1;
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if ( root == null )
return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
if ( left > right )
return left + 1;
return right + 1;
}
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
if left < right {
return right + 1
}
return left + 1
}