zoukankan      html  css  js  c++  java
  • [LeetCode] Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

    The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

    Example 1:

    Input: Binary tree: [1,2,3,4]
           1
         /   
        2     3
       /    
      4     
    
    Output: "1(2(4))(3)"
    
    Explanation: Originallay 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)".

    Example 2:

    Input: Binary tree: [1,2,3,null,4]
           1
         /   
        2     3
           
          4 
    
    Output: "1(2()(4))(3)"
    
    Explanation: Almost the same as the first example,
    except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.

    通过二叉树构造字符串,这道题的关键在于括号的判断,括号的添加可以分为以下4种情况:

    1.  节点的左右孩子都存在,添加括号在节点值的两侧。

    2. 节点的左右孩子都不存在,无需添加括号。

    3. 节点的左孩子存在有孩子不存在,无需在右孩子位置添加括号。

    4. 节点的左孩子不存在右孩子存在,则需要在左孩子位置添加括号。

    总的来说就是如果节点左右孩子都不存在,则忽略括号。如果节点有左孩子则一定要在它两侧添加括号。如果左孩子不存在时右孩子存在,则在左孩子位置添加括号。如果节点右孩子存在,则在右孩子两侧添加括号。

    class Solution {
    public:
        string tree2str(TreeNode* t) {
            if (t == nullptr)
                return "";
            string s = to_string(t->val);
            if (t->left != nullptr)
                s += "(" + tree2str(t->left) + ")";
            else if (t->right != nullptr)
                s += "()";
            if (t->right != nullptr)
                s += "(" + tree2str(t->right) + ")";
            return s;
        }
    };
    // 18 ms

     通过使用stack来处理当前节点,set来判断当前节点是否处理过。来进行迭代。如果当前节点没有处理,则先加入左括号后判断它的左右孩子节点,判断的3个条件如上解法。如果当前节点处理过,则要在结果字符串中加入右括号。最后去掉结果字符串中首位和末位括号即可。

    class Solution {
    public:
        string str;
        string tree2str(TreeNode* t) {
            if (t == nullptr)
                return "";
            stack<TreeNode*> stk;
            set<TreeNode*> visited;
            stk.push(t);
            while (!stk.empty()) {
                t = stk.top();
                if (visited.count(t)) {
                    stk.pop();
                    str += ")";
                }
                else {
                    visited.insert(t);
                    str += "(" + to_string(t->val);
                    if (t->left == nullptr && t->right != nullptr)
                        str += "()";
                    if (t->right != nullptr)
                        stk.push(t->right);
                    if (t->left != nullptr)
                        stk.push(t->left);
                }
            }
            return str.substr(1, str.size() - 2);
        }
    };
    // 22 ms
  • 相关阅读:
    Mybatis多层嵌套查询
    UUID 唯一性实现原理
    oracle 多实例启动
    orcal启动多实例是报 ORA-00845: MEMORY_TARGET not supported onthis system
    java调用quartz 2.2.2方法总结。
    mybatis中like的使用(模糊查询)
    Orcal数据库实现主键ID自增
    spring cloud分布式关于熔断器
    spring cloud分布式健康检查
    spring cloud分布式整合zipkin的链路跟踪
  • 原文地址:https://www.cnblogs.com/immjc/p/7154413.html
Copyright © 2011-2022 走看看