337. House Robber III Medium

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



1/**
2 * [337] House Robber III
3 *
4 * The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.
5 * Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
6 * Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" />
10 * Input: root = [3,2,3,null,3,null,1]
11 * Output: 7
12 * Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" />
16 * Input: root = [3,4,5,1,3,null,1]
17 * Output: 9
18 * Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	The number of nodes in the tree is in the range [1, 10^4].
24 * 	0 <= Node.val <= 10^4
25 * 
26 */
27pub struct Solution {}
28use crate::util::tree::{TreeNode, to_tree};
29
30// problem: https://leetcode.com/problems/house-robber-iii/
31// discuss: https://leetcode.com/problems/house-robber-iii/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for a binary tree node.
36// #[derive(Debug, PartialEq, Eq)]
37// pub struct TreeNode {
38//   pub val: i32,
39//   pub left: Option<Rc<RefCell<TreeNode>>>,
40//   pub right: Option<Rc<RefCell<TreeNode>>>,
41// }
42// 
43// impl TreeNode {
44//   #[inline]
45//   pub fn new(val: i32) -> Self {
46//     TreeNode {
47//       val,
48//       left: None,
49//       right: None
50//     }
51//   }
52// }
53use std::rc::Rc;
54use std::cell::RefCell;
55impl Solution {
56    pub fn rob(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_337() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.