leetcode606

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode606相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public string Tree2str(TreeNode t) {
        if (t == null)
            {
                return "";
            }

            string result = t.val + "";

            string left = Tree2str(t.left);
            string right = Tree2str(t.right);

            if (left == "" && right == "")
            {
                return result;
            }
            if (left == "")
            {
                return result + "()" + "(" + right + ")";
            }
            if (right == "")
            {
                return result + "(" + left + ")";
            }
            return result + "(" + left + ")" + "(" + right + ")";
    }
}

https://leetcode.com/problems/construct-string-from-binary-tree/#/description

以上是关于leetcode606的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 606 根据二叉树创建字符串[二叉树 dfs] HERODING的LeetCode之路

leetcode606

[leetcode-606-Construct String from Binary Tree]

LeetCode 606. Construct String from Binary Tree

LeetCode 606. Construct String from Binary Tree

[LeetCode&Python] Problem 606. Construct String from Binary Tree