zoukankan      html  css  js  c++  java
  • [Algorithm] 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.

    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)".
    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.
    function Node(val) {
      return {
        val,
        left: null,
        right: null
      };
    }
    
    function Tree() {
      return {
        root: null,
        addLeft(val, root) {
          const node = Node(val);
          root.left = node;
          return root.left;
        },
        addRight(val, root) {
          const node = Node(val);
          root.right = node;
          return root.right;
        }
      };
    }
    
    function printTree(root) {
      let result = "";
      function print(root, result = '') {
        if (root === null) {
          return "";
        }
    
        // the leaf node
        if (root.left === null && root.right === null) {
          return `${root.val}`;
        }
    
        //if has left but no right
        if (root.left !== null && root.right === null) {
          return `${root.val}(${root.left.val})`;
        }
    
        // if has no left but has right
        if (root.left === null && root.right !== null) {
          return `${root.val}()(${root.right.val})`;
        }
        
        result += root.val
        result += `(${print(root.left, result)})`;
        result += `(${print(root.right, result)})`;
        return result
      }
    
      result += print(root, result);
      
      return result;
    }
    
    const t1 = new Tree();
    t1.root = Node(1);
    const n1 = t1.addLeft(2, t1.root);
    t1.addRight(3, t1.root);
    t1.addLeft(4, n1);
    console.log(printTree(t1.root)); // '1(2(4))(3)'
    
    const t2 = new Tree();
    t2.root = Node(1);
    const n2 = t1.addLeft(2, t2.root);
    t2.addRight(3, t2.root);
    t2.addRight(4, n2);
    console.log(printTree(t2.root)); //'1(2()(4))(3)'
  • 相关阅读:
    操作数组可以通过Array这个类来操作(不需要考虑数组的类型!!!)
    Servlet------>jsp自定义标签SimpleTag(jsp2.0以后的方法,1-5已经淘汰了)
    Servlet------>jsp自定义标签5(标签体内容改为大写)
    Servlet------>jsp自定义标签(JSPTAG接口)
    Servlet------>jsp自定义标签4(重复标签体)
    Servlet------>jsp自定义标签3(不显示余下jsp内容)
    模块学习
    正则表达式与re模块
    模块
    迭代器与生成器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10663093.html
Copyright © 2011-2022 走看看