606. Construct String from Binary Tree Easy

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



1/**
2 * [606] Construct String from Binary Tree
3 *
4 * Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
5 * Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/cons1-tree.jpg" style="width: 292px; height: 301px;" />
9 * Input: root = [1,2,3,4]
10 * Output: "1(2(4))(3)"
11 * Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/cons2-tree.jpg" style="width: 207px; height: 293px;" />
15 * Input: root = [1,2,3,null,4]
16 * Output: "1(2()(4))(3)"
17 * Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	The number of nodes in the tree is in the range [1, 10^4].
23 * 	-1000 <= Node.val <= 1000
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/construct-string-from-binary-tree/
30// discuss: https://leetcode.com/problems/construct-string-from-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn tree2str(root: Option<Rc<RefCell<TreeNode>>>) -> String {
56        String::new()
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_606() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.