Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (史萊哲林的優等生)時間2年前 (2023/07/14 16:47), 編輯推噓1(101)
留言2則, 2人參與, 2年前最新討論串368/719 (看更多)
用Easy來玩Rust 104. Maximum Depth of Binary Tree Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Constraints: The number of nodes in the tree is in the range [0, 104]. -100 <= Node.val <= 100 Code: use std::cell::RefCell; use std::rc::Rc; impl Solution { pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { match root { Some(node) =>{ let mut node = node.borrow_mut(); let left_depth = Self::max_depth(node.left.clone()); let right_depth = Self::max_depth(node.right.clone()); return std::cmp::max(left_depth, right_depth) + 1; } None => return 0, } return 0; } } 等價於: class Solution { public: int maxDepth(TreeNode* root) { if (root == nullptr) return 0; int right_depth = maxDepth(root->right); int left_depth = maxDepth(root->left); return max(right_depth, left_depth) + 1; } }; 這到底什麼語言 我落淚了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.163 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1689324430.A.F5F.html

07/14 17:04, 2年前 , 1F
這沒法再簡化了喔
07/14 17:04, 1F

07/14 17:06, 2年前 , 2F
rust:00
07/14 17:06, 2F
文章代碼(AID): #1aiGkEzV (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1aiGkEzV (Marginalman)