码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode606

时间:2017-06-05 10:28:12      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:turn   rom   for   ems   problems   color   solution   res   esc   

/**
 * 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

标签:turn   rom   for   ems   problems   color   solution   res   esc   

原文地址:http://www.cnblogs.com/asenyang/p/6943238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!