Java tree travel -- Beat 100%

  • 0

    The idea is to recursive travel the tree. The logic is similar as others, but it is beat 100%. Is it because of using "isEmpty()" method instead of using "equals()"?

    public class Solution {
        public String tree2str(TreeNode t) {
                return "";
            String left = tree2str(t.left);
            String right = tree2str(t.right);
            if(left.isEmpty() && right.isEmpty()){
                return t.val+"";
            }else if(left.isEmpty()){
                return t.val +"()("+right+")";
            }else if(right.isEmpty()){
                return t.val + "("+left+")";
                return t.val + "("+ left + ")(" + right+ ")";

Log in to reply

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