671. Second Minimum Node In a Binary Tree Easy

@problem@discussion
#Tree#Depth-First Search#Binary Tree



1/**
2 * [671] Second Minimum Node In a Binary Tree
3 *
4 * Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
5 * Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
6 * If no such second minimum value exists, output -1 instead.
7 *  
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt1.jpg" style="width: 431px; height: 302px;" />
11 * Input: root = [2,2,5,null,null,5,7]
12 * Output: 5
13 * Explanation: The smallest value is 2, the second smallest value is 5.
14 * 
15 * Example 2:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt2.jpg" style="width: 321px; height: 182px;" />
17 * Input: root = [2,2,2]
18 * Output: -1
19 * Explanation: The smallest value is 2, but there isn't any second smallest value.
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	The number of nodes in the tree is in the range [1, 25].
25 * 	1 <= Node.val <= 2^31 - 1
26 * 	root.val == min(root.left.val, root.right.val) for each internal node of the tree.
27 * 
28 */
29pub struct Solution {}
30use crate::util::tree::{TreeNode, to_tree};
31
32// problem: https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
33// discuss: https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37// Definition for a binary tree node.
38// #[derive(Debug, PartialEq, Eq)]
39// pub struct TreeNode {
40//   pub val: i32,
41//   pub left: Option<Rc<RefCell<TreeNode>>>,
42//   pub right: Option<Rc<RefCell<TreeNode>>>,
43// }
44// 
45// impl TreeNode {
46//   #[inline]
47//   pub fn new(val: i32) -> Self {
48//     TreeNode {
49//       val,
50//       left: None,
51//       right: None
52//     }
53//   }
54// }
55use std::rc::Rc;
56use std::cell::RefCell;
57impl Solution {
58    pub fn find_second_minimum_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
59        0
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_671() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.