1022. Sum of Root To Leaf Binary Numbers Easy

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



1/**
2 * [1022] Sum of Root To Leaf Binary Numbers
3 *
4 * You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.
5 * 
6 * 	For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
7 * 
8 * For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
9 * The test cases are generated so that the answer fits in a 32-bits integer.
10 *  
11 * Example 1:
12 * <img alt="" src="https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png" style="width: 400px; height: 263px;" />
13 * Input: root = [1,0,1,0,1,0,1]
14 * Output: 22
15 * Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
16 * 
17 * Example 2:
18 * 
19 * Input: root = [0]
20 * Output: 0
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	The number of nodes in the tree is in the range [1, 1000].
26 * 	Node.val is 0 or 1.
27 * 
28 */
29pub struct Solution {}
30use crate::util::tree::{TreeNode, to_tree};
31
32// problem: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
33// discuss: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/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 sum_root_to_leaf(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_1022() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.