• [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
  • 相关阅读:
    《编写高质量代码》读书笔记
    IL,Emit之OpCodes说明(备查)
    每天进步一点点
    DOCTYPE 与浏览器模式分析[转]
    一个EL IF表达式引发的血案
    JVM系列文章
    SunOs设置环境变量
    Solaris系统查看进程与端口对应关系的命令
    jsp 报错调试
    vi命令提示:Terminal too wide
  • 原文地址:https://www.cnblogs.com/immjc/p/7154413.html
Copyright © 2020-2023  润新知