Java, recursive tree traversal using stringbuilder


  • 0
    M
    public class Solution {
        public String tree2str(TreeNode t) {
            StringBuilder sb = new StringBuilder();
            helper(t, sb);
            return sb.toString();
        }
        private void helper(TreeNode t, StringBuilder sb) {
            if (t == null) return;
            sb.append(t.val);
            if (t.left != null || t.right != null) {
                sb.append("(");
                helper(t.left, sb);
                sb.append(")");
            }
            if (t.right != null) {
                sb.append("(");
                helper(t.right, sb);
                sb.append(")");
            }
        }
    }
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.