• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

算法leetcode|剑指 Offer 55 - I. 二叉树的深度|104. 二叉树的最大深度rust和go

武飞扬头像
二当家的白帽子
帮助1



剑指 Offer 55 - I. 二叉树的深度|104. 二叉树的最大深度:

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

样例 1:

给定二叉树 [3,9,20,null,null,15,7],
	
	3
   / \
  9  20
    /  \
   15   7
	
返回它的最大深度 3 。

提示:

  • 节点总数 <= 10000

分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 使用层序遍历或者递归都可以,感觉递归套娃大法更加直观。

题解

rust

// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
        match root {
            Some(root) => {
                1   Solution::max_depth(root.borrow().left.clone()).max(Solution::max_depth(root.borrow().right.clone()))
            }
            _ => {
                0
            }
        }
    }
}
学新通

go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root != nil {
		maxLeftDepth := maxDepth(root.Left)
		maxRightDepth := maxDepth(root.Right)
		if maxLeftDepth > maxRightDepth {
			return 1   maxLeftDepth
		} else {
			return 1   maxRightDepth
		}
	}
	return 0
}
学新通

c

/**
 * 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) {
        if (root) {
            return 1   max(maxDepth(root->left), maxDepth(root->right));
        }
        return 0;
    }
};
学新通

java

/**
 * 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 1   Math.max(maxDepth(root.left), maxDepth(root.right));
		}
		return 0;
    }
}
学新通

python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root:
            return 1   max(self.maxDepth(root.left), self.maxDepth(root.right))
        return 0


原题传送门:https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/

原题传送门:https://leetcode.cn/problems/maximum-depth-of-binary-tree/



这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhfehfh