题解 | #二叉树的深度# | Rust
二叉树的深度
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option<Box<TreeNode>>, * pub right: Option<Box<TreeNode>>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot TreeNode类 * @return int整型 */ pub fn TreeDepth(&self, pRoot: Option<Box<TreeNode>>) -> i32 { if pRoot.is_none() { return 0; } return 1 + std::cmp::max(Solution::TreeDepth(self, pRoot.as_ref().unwrap().left.clone()), Solution::TreeDepth(self, pRoot.as_ref().unwrap().right.clone())); } }