https://leetcode.com/problems/construct-string-from-binary-tree
- use equals
https://leetcode.com/articles/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.
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.https://discuss.leetcode.com/topic/91308/java-solution-tree-traversal
- use equals
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 + ")";
}
public String tree2str(TreeNode t) {
if(t == null) return "";
String left = tree2str(t.left);
String right = tree2str(t.right);
if(left == "" && right == "") return t.val + "";
return t.val + "(" + left + ")" + (right == "" ? "" : "(" + right + ")");
}
https://discuss.leetcode.com/topic/91311/java-simple-recursion 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(")");
}
}
}
}
https://discuss.leetcode.com/topic/91315/java-recursion-stringbuilderpublic static String tree2str(TreeNode t) {
if(t == null) return "";
return tree2str1(t).toString();
}
public static StringBuilder tree2str1(TreeNode t) {
if(t == null) return null;
StringBuilder sb = new StringBuilder();
sb.append(t.val);
StringBuilder left = tree2str1(t.left);
StringBuilder right = tree2str1(t.right);
if(right == null && left == null) return sb;
sb.append("(").append(left == null ? "" : left).append(")");
if(right != null) sb.append("(").append(right).append(")");
return sb;
}
https://segmentfault.com/a/1190000016966243 public String tree2str(TreeNode t) {
if (t == null) return "";
String str = String.valueOf(t.val);
if (t.left != null) {
str += "(" + tree2str(t.left) + ")";
}
if (t.right != null) {
if (t.left == null) str += "()";
str += "(" + tree2str(t.right) + ")";
}
return str;
}
https://github.com/cherryljr/LeetCode/blob/master/Construct%20String%20from%20Binary%20Tree.java
public String tree2str(TreeNode t) {
if (t == null) {
return "";
}
if (t.left == null && t.right == null) {
return t.val + "";
}
if (t.right == null) {
return t.val + "(" + tree2str(t.left) + ")";
}
return t.val + "(" + tree2str(t.left) + ")" + "(" + tree2str(t.right) + ")";
}
http://www.cnblogs.com/grandyang/p/7000040.htmlstring tree2str(TreeNode* t) { if (!t) return ""; string res = to_string(t->val); if (!t->left && !t->right) return res; res += "(" + tree2str(t->left) + ")"; if (t->right) res += "(" + tree2str(t->right) + ")"; return res; }X. Using Stack
https://leetcode.com/articles/construct-string-from-binary-tree/
public String tree2str(TreeNode t) { if (t == null) return ""; Stack < TreeNode > stack = new Stack < > (); stack.push(t); Set < TreeNode > visited = new HashSet < > (); StringBuilder s = new StringBuilder(); while (!stack.isEmpty()) { t = stack.peek(); if (visited.contains(t)) { stack.pop(); s.append(")"); } else { visited.add(t); s.append("(" + t.val); if (t.left == null && t.right != null) s.append("()"); if (t.right != null) stack.push(t.right); if (t.left != null) stack.push(t.left); } } return s.substring(1, s.length() - 1); }