another way to recursion

  • 1

    don't need to record max...

    public class Solution {
        public TreeNode constructMaximumBinaryTree(int[] nums) {
            if(nums == null) return null;
            TreeNode root = null;
            for(int i: nums)
                root = helper(root,i);
            return root;
        private TreeNode helper(TreeNode x, int i){
            if(x == null) return new TreeNode(i);
            if(x.val > i) 
                x.right = helper(x.right,i);
                TreeNode root = new TreeNode(i);
                root.left = x;
                return root;
            return x;

Log in to reply

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