2019-11-10 LeetCode in Rust LeetCode 549. Binary Tree Longest Consecutive Sequence II Problem Statement use std::rc::Rc;use std::cell::RefCell;impl Solution { fn dfs(root: Option<Rc<RefCell<TreeNode>>>, parent: Option<Rc<RefCell<TreeNode>>>, ans: &mut i32) -> (i32, i32) { if let Some(node) = root { let (mut incr, mut decr) = (0, 0); let l = Self::dfs(node.borrow().left.clone(), Some(node.clone()), ans); let r = Self::dfs(node.borrow().right.clone(), Some(node.clone()), ans); *ans = std::cmp::max(*ans, std::cmp::max(l.0 + r.1 + 1, l.1 + r.0 + 1)); let p = parent.unwrap(); if node.borrow().val == p.borrow().val + 1 { incr = std::cmp::max(l.0, r.0) + 1; } if node.borrow().val + 1 == p.borrow().val { decr = std::cmp::max(l.1, r.1) + 1; } return (incr, decr); } else { return (0, 0); } } pub fn longest_consecutive(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { let mut ans = 0; Self::dfs(root.clone(), root.clone(), &mut ans); return ans; }} Newer LeetCode 523. Continuous Subarray Sum Older LeetCode 556. Next Greater Element III