数据结构不太会,把答案记下来,以便以后看
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode pre = null;
public String tree2str(TreeNode t) {
if(t == null) return "";
StringBuilder sb = new StringBuilder();
ha(t,pre,sb);
return sb.substring(1 , sb.length() - 1);
}
public void ha(TreeNode root,TreeNode pre,StringBuilder sb){
if(root == null) return ;
if(pre != null && pre.left == null && pre.right ==root)
sb.append("()");
sb.append("(").append(root.val);
pre = root;
ha(root.left,pre,sb);
ha(root.right,pre,sb);
sb.append(")");
}
}