C#LeetCode刷题之#606-根据二叉树创建字符串(Construct String from Binary Tree)

C#LeetCode刷题之#606-根据二叉树创建字符串(Construct String from Binary Tree)

问题

你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。

空节点则用一对空括号 “()” 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。

输入: 二叉树: [1,2,3,4]
       1
     /   \
    2     3
   /    
  4     

输出: “1(2(4))(3)”

解释: 原本将是“1(2(4)())(3())”,在你省略所有不必要的空括号对之后,它将是“1(2(4))(3)”。

输入: 二叉树: [1,2,3,null,4]
       1
     /   \
    2     3
     \  
      4 

输出: “1(2()(4))(3)”

解释: 和第一个示例相似,除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

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.

示例

public class Program {

    public static void Main(string[] args) {
        var t = new TreeNode(1) {
            left = new TreeNode(2),
            right = new TreeNode(3)
        };
        t.left.left = new TreeNode(4);

        var res = Tree2str(t);
        Console.WriteLine(res);

        t = new TreeNode(1) {
            left = new TreeNode(2),
            right = new TreeNode(3)
        };
        t.left.right = new TreeNode(4);

        res = Tree2str2(t);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    private static string Tree2str(TreeNode t) {
        if(t == null) return "";
        var res = t.val.ToString();
        if(t.left != null) {
            res += '(' + Tree2str(t.left) + ')';
        } else if(t.right != null) {
            res += "()";
        }
        if(t.right != null) res += '(' + Tree2str(t.right) + ')';
        return res;
    }

    private static string Tree2str2(TreeNode t) {
        var sb = new StringBuilder();
        SearchAndAdd(t, sb);
        return sb.ToString();
    }

    private static void SearchAndAdd(TreeNode t, StringBuilder sb) {
        if(t == null) return;
        sb.Append(t.val);
        if(t.left != null) {
            sb.Append("(");
            SearchAndAdd(t.left, sb);
            sb.Append(")");
        }
        if(t.right != null) {
            if(t.left == null) sb.Append("()");
            sb.Append("(");
            SearchAndAdd(t.right, sb);
            sb.Append(")");
        }
    }

    public class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int x) { val = x; }
    }

}

以上给出2种算法实现,以下是这个案例的输出结果:

1(2(4))(3)
1(2()(4))(3)

分析:

显而易见,以上2种算法的时间复杂度均为: O(n)​ 。

本文由 .Net中文网 原创发布,欢迎大家踊跃转载。

转载请注明本文地址:https://www.byteflying.com/archives/4094

发表评论

登录后才能评论