C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
}
};
运行代码
提交
golang 解法, 执行用时: 4 ms, 内存消耗: 4.3 MB, 提交时间: 2024-02-20 09:57:46
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func calculateDepth(root *TreeNode) (ans int) {
var dfs func(*TreeNode, int)
dfs = func(node *TreeNode, cnt int) {
if node == nil {
return
}
cnt++
ans = max(ans, cnt)
dfs(node.Left, cnt)
dfs(node.Right, cnt)
}
dfs(root, 0)
return
}
func max(a, b int) int { if b > a { return b }; return a }
python3 解法, 执行用时: 45 ms, 内存消耗: 17.8 MB, 提交时间: 2024-02-20 09:56:53
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 不用全局变量
def calculateDepth(self, root: Optional[TreeNode]) -> int:
if root is None: return 0
l_depth = self.calculateDepth(root.left)
r_depth = self.calculateDepth(root.right)
return max(l_depth, r_depth) + 1
# 用全局变量
def calculateDepth2(self, root: Optional[TreeNode]) -> int:
ans = 0
def dfs(node, cnt):
if node is None:
return
cnt += 1
nonlocal ans
ans = max(ans, cnt)
dfs(node.left, cnt)
dfs(node.right, cnt)
dfs(root, 0)
return ans
php 解法, 执行用时: 16 ms, 内存消耗: 16.8 MB, 提交时间: 2021-05-10 17:15:23
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @return Integer
*/
function maxDepth($root) {
if ( $root == null ) return 0;
$res = 0;
$queue = [$root];
while ( !empty($queue) ) {
$cnt = count($queue);
for ( $i = 0; $i < $cnt; $i++ ) {
$node = array_shift($queue);
if ( $node->left ) $queue[] = $node->left;
if ( $node->right ) $queue[] = $node->right;
}
$res++;
}
return $res; // 元素个数即为二叉树的深度
}
}
php 解法, 执行用时: 12 ms, 内存消耗: 17.1 MB, 提交时间: 2021-05-10 17:10:40
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @return Integer
*/
function maxDepth($root) {
if ( $root == null ) return 0;
return max($this->maxDepth($root->left), $this->maxDepth($root->right)) + 1;
}
}