Java why not more easier(beats 90.75%)?


  • 0
    W

    just use preOrder to String.
    String to TreeNode, base on the construct rule of BST

    public class Codec {
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            if(root == null){
                return "";
            }
            StringBuilder sb = new StringBuilder();
            preOrder(root, sb);
            String data = sb.toString();
            int len = data.length();
            return data.substring(0, len - 1);
        }
        
        private void preOrder(TreeNode root, StringBuilder sb){
            if(root == null){
                return;
            }
            sb.append(String.valueOf(root.val));
            sb.append(",");
            preOrder(root.left, sb);
            preOrder(root.right, sb);
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            if(data == null || "".equals(data)){
                return null;
            }
            String array[] = data.split(",");
            return constructTree(array, 0, array.length);
        }
        
        private TreeNode constructTree(String array[], int l, int r){
            if(l >= r){
                return null;
            }
            int val = Integer.parseInt(array[l]);
            TreeNode root = new TreeNode(val);
            int k = l + 1;
            while(k < r && Integer.parseInt(array[k]) < val){
                k++;
            }
            root.left = constructTree(array, l + 1, k);
            root.right = constructTree(array, k, r);
            return root;
        }
    }
    
    

Log in to reply
 

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