606. Construct String from Binary Tree 【easy】

606. Construct String from Binary Tree 【easy】

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.

差一点AC的代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string tree2str(TreeNode* t) {
string result; if (t == NULL) {
return "";
} result += to_string(t->val); if (t->left) {
result += "(" + tree2str(t->left) + ")";
} if (t->right) {
result += "(" + tree2str(t->right) + ")";
} return result;
}
};

606. Construct String from Binary Tree 【easy】

实际上对应的图如下图,可以发现需要对左子树为空,右子树不为空的情况做个特殊判断。

606. Construct String from Binary Tree 【easy】

解法一:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string tree2str(TreeNode* t) {
string result; if (t == NULL) {
return "";
} result += to_string(t->val); if (t->left) {
result += "(" + tree2str(t->left) + ")";
}
else if (t->right) {
result += "()";
} if (t->right) {
result += "(" + tree2str(t->right) + ")";
} return result;
}
};

上面代码中第24 ~ 26行就是对左子树为空,右子树不为空的情况做的特殊判断。

解法二:

 class Solution {
public:
string tree2str(TreeNode* t) {
return !t ? "" : to_string(t->val) + (t->left ? "(" + tree2str(t->left) + ")" : t->right ? "()" : "")
+ (t->right ? "(" + tree2str(t->right) + ")" : "");
}
};

参考@alexander 的代码。

解法三:

 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 + ")";
}
}

最后集中起来再判断的思路很好,参考@shawngao 的代码。

解法四:

 public String tree2str(TreeNode t) {
StringBuilder sb = new StringBuilder();
helper(sb, t);
return sb.toString();
}
public void helper(StringBuilder sb, TreeNode t) {
if (t != null) {
sb.append(t.val); if(t.left != null || t.right != null) {
sb.append("(");
helper(sb, t.left);
sb.append(")"); if(t.right != null) {
sb.append("(");
helper(sb, t.right);
sb.append(")");
}
}
}
}
上一篇:【leetcode】572. Subtree of Another Tree


下一篇:Mac 使用Sublime Text 3搭建java环境