recursion vs iteration time complexity. Both recursion and iteration run a chunk of code until a stopping condition is reached. recursion vs iteration time complexity

 
 Both recursion and iteration run a chunk of code until a stopping condition is reachedrecursion vs iteration time complexity Recursion tree and substitution method

A recursive structure is formed by a procedure that calls itself to make a complete performance, which is an alternate way to repeat the process. Its time complexity anal-ysis is similar to that of num pow iter. Loops are almost always better for memory usage (but might make the code harder to. 1 Answer Sorted by: 4 Common way to analyze big-O of a recursive algorithm is to find a recursive formula that "counts" the number of operation done by. It also covers Recursion Vs Iteration: From our earlier tutorials in Java, we have seen the iterative approach wherein we declare a loop and then traverse through a data structure in an iterative manner by taking one element at a time. Time & Space Complexity of Iterative Approach. This is the recursive method. So, let’s get started. So, this gets us 3 (n) + 2. Recursive — Inorder Complexity: Time: O(n) / Space: O(h), height of tree, best:. Each of the nested iterators, will also only return one value at a time. , current = current->right Else a) Find. There is less memory required in the case of. In data structure and algorithms, iteration and recursion are two fundamental problem-solving approaches. As an example of the above consideration, a sum of subset problem can be solved using both recursive and iterative approach but the time complexity of the recursive approach is O(2N) where N is. Program for Tower of Hanoi Algorithm; Time Complexity Analysis | Tower Of Hanoi (Recursion) Find the value of a number raised to its reverse; Recursively remove all adjacent duplicates; Print 1 to n without using loops; Print N to 1 without loop; Sort the Queue using Recursion; Reversing a queue using. Your stack can blow-up if you are using significantly large values. Storing these values prevent us from constantly using memory. Computations using a matrix of size m*n have a space complexity of O (m*n). Iteration uses the permanent storage area only for the variables involved in its code block and therefore memory usage is relatively less. Exponential! Ew! As a rule of thumb, when calculating recursive runtimes, use the following formula: branches^depth. The problem is converted into a series of steps that are finished one at a time, one after another. O (NW) in the knapsack problem. def tri(n: Int): Int = { var result = 0 for (count <- 0 to n) result = result + count result} Note that the runtime complexity of this algorithm is still O(n) because we will be required to iterate n times. Time Complexity: It has high time complexity. Here are the general steps to analyze the complexity of a recurrence relation: Substitute the input size into the recurrence relation to obtain a sequence of terms. Recursive calls don't cause memory "leakage" as such. W hat I will be discussing in this blog is the difference in computational time between different algorithms to get Fibonacci numbers and how to get the best results in terms of time complexity using a trick vs just using a loop. The iterative solution has three nested loops and hence has a complexity of O(n^3) . Recursion does not always need backtracking. An iterative implementation requires, in the worst case, a number. g. The space complexity can be split up in two parts: The "towers" themselves (stacks) have a O (𝑛) space complexity. A single point of comparison has a bias towards the use-case of recursion and iteration, in this case; Iteration is much faster. Learn more about recursion & iteration, differences, uses. 3. "use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n)=T (n/2)+n^2. Only memory for the. "tail recursion" and "accumulator based recursion" are not mutually exclusive. Use a substitution method to verify your answer". The time complexity of the method may vary depending on whether the algorithm is implemented using recursion or iteration. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. There are O(N) iterations of the loop in our iterative approach, so its time complexity is also O(N). Iteration will be faster than recursion because recursion has to deal with the recursive call stack frame. Loops do not. In the above algorithm, if n is less or equal to 1, we return nor make two recursive calls to calculate fib of n-1 and fib of n-2. The Java library represents the file system using java. Hence it’s space complexity is O (1) or constant. The inverse transformation can be trickier, but most trivial is just passing the state down through the call chain. With iteration, rather than building a call stack you might be storing. Calculate the cost at each level and count the total no of levels in the recursion tree. Iteration vs. Iterative and recursive both have same time complexity. Recursion may be easier to understand and will be less in the amount of code and in executable size. Python. The first method calls itself recursively once, therefore the complexity is O(n). ). Recursion is inefficient not because of the implicit stack but because of the context switching overhead. Applying the Big O notation that we learn in the previous post , we only need the biggest order term, thus O (n). 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack. Utilization of Stack. That’s why we sometimes need to. University of the District of Columbia. Generally, it has lower time complexity. In the factorial example above, we have reached the end of our necessary recursive calls when we get to the number 0. First, let’s write a recursive function:Reading time: 35 minutes | Coding time: 15 minutes. If the number of function. Here, the iterative solution. Now, an obvious question is: if a tail-recursive call can be optimized the same way as a. Recursion can increase space complexity, but never decreases. To estimate the time complexity, we need to consider the cost of each fundamental instruction and the number of times the instruction is executed. Space Complexity : O(2^N) This is due to the stack size. g. Each function call does exactly one addition, or returns 1. In. Step1: In a loop, calculate the value of “pos” using the probe position formula. Loops are generally faster than recursion, unless the recursion is part of an algorithm like divide and conquer (which your example is not). Calculate the cost at each level and count the total no of levels in the recursion tree. base case) Update - It gradually approaches to base case. We would like to show you a description here but the site won’t allow us. In the above recursion tree diagram where we calculated the fibonacci series in c using the recursion method, we. On the other hand, iteration is a process in which a loop is used to execute a set of instructions repeatedly until a condition is met. Let a ≥ 1 and b > 1 be constants, let f ( n) be a function, and let T ( n) be a function over the positive numbers defined by the recurrence. e. The towers of Hanoi problem is hard no matter what algorithm is used, because its complexity is exponential. It's a equation or a inequality that describes a functions in terms of its values and smaller inputs. Difference in terms of code a nalysis In general, the analysis of iterative code is relatively simple as it involves counting the number of loop iterations and multiplying that by the. It is a technique or procedure in computational mathematics used to solve a recurrence relation that uses an initial guess to generate a sequence of improving approximate solutions for a class of. Time Complexity: O(n) Space Complexity: O(1) Note: Time & Space Complexity is given for this specific example. 1. Recursion is more natural in a functional style, iteration is more natural in an imperative style. Comparing the above two approaches, time complexity of iterative approach is O(n) whereas that of recursive approach is O(2^n). : f(n) = n + f(n-1) •Find the complexity of the recurrence: –Expand it to a summation with no recursive term. 1. Strictly speaking, recursion and iteration are both equally powerful. Table of contents: Introduction; Types of recursion; Non-Tail Recursion; Time and Space Complexity; Comparison between Non-Tail Recursion and Loop; Tail Recursion vs. Generally, it has lower time complexity. The time complexity is lower as compared to. Sometimes it's even simpler and you get along with the same time complexity and O(1) space use instead of, say, O(n) or O(log n) space use. If you're wondering about computational complexity, see here. 1. Processes generally need a lot more heap space than stack space. 1 Answer. ago. Iteration: An Empirical Study of Comprehension Revisited. When a function is called recursively the state of the calling function has to be stored in the stack and the control is passed to the called function. Iteration is almost always the more obvious solution to every problem, but sometimes, the simplicity of recursion is preferred. Recursion adds clarity and (sometimes) reduces the time needed to write and debug code (but doesn't necessarily reduce space requirements or speed of execution). So whenever the number of steps is limited to a small. 2. 5: We mostly prefer recursion when there is no concern about time complexity and the size of code is. Iteration is the process of repeatedly executing a set of instructions until the condition controlling the loop becomes false. Conclusion. It is faster because an iteration does not use the stack, Time complexity. No. No. Strictly speaking, recursion and iteration are both equally powerful. The advantages of. If n == 1, then everything is trivial. Time Complexity: O(n), a vast improvement over the exponential time complexity of recursion. Recursion vs Iteration is one of those age-old programming holy wars that divides the dev community almost as much as Vim/Emacs, Tabs/Spaces or Mac/Windows. For example, using a dict in Python (which has (amortized) O (1) insert/update/delete times), using memoization will have the same order ( O (n)) for calculating a factorial as the basic iterative solution. This is the iterative method. Apart from the Master Theorem, the Recursion Tree Method and the Iterative Method there is also the so called "Substitution Method". Iteration: Generally, it has lower time complexity. Found out that there exists Iterative version of Merge Sort algorithm with same time complexity but even better O(1) space complexity. Time complexity. 1. ) Every recursive algorithm can be converted into an iterative algorithm that simulates a stack on which recursive function calls are executed. Explaining a bit: we know that any. This reading examines recursion more closely by comparing and contrasting it with iteration. Other methods to achieve similar objectives are Iteration, Recursion Tree and Master's Theorem. The second method calls itself recursively two times, so per recursion depth the amount of calls is doubled, which makes the method O(2 n). While studying about Merge Sort algorithm, I was curious to know if this sorting algorithm can be further optimised. Iteration Often what is. One uses loops; the other uses recursion. "Recursive is slower then iterative" - the rational behind this statement is because of the overhead of the recursive stack (saving and restoring the environment between calls). Knowing the time complexity of a method involves examining whether you have implemented an iteration algorithm or. A recurrence is an equation or inequality that describes a function in terms of its values on smaller inputs. But when you do it iteratively, you do not have such overhead. 5: We mostly prefer recursion when there is no concern about time complexity and the size of code is small. There are often times that recursion is cleaner, easier to understand/read, and just downright better. Iteration: "repeat something until it's done. However -these are constant number of ops, while not changing the number of "iterations". e. 2. In the next pass you have two partitions, each of which is of size n/2. e. If I do recursive traversal of a binary tree of N nodes, it will occupy N spaces in execution stack. You will learn about Big O(2^n)/ exponential growt. To visualize the execution of a recursive function, it is. Recursion takes longer and is less effective than iteration. Introduction. Analyzing the time complexity for our iterative algorithm is a lot more straightforward than its recursive counterpart. The time complexity is lower as compared to. )Time complexity is very useful measure in algorithm analysis. Iteration is generally faster, some compilers will actually convert certain recursion code into iteration. And I have found the run time complexity for the code is O(n). Can have a fixed or variable time complexity depending on the number of recursive calls. So does recursive BFS. For example, use the sum of the first n integers. We. Generally, it has lower time complexity. Before going to know about Recursion vs Iteration, their uses and difference, it is very important to know what they are and their role in a program and machine languages. Introduction. Tail-recursion is the intersection of a tail-call and a recursive call: it is a recursive call that also is in tail position, or a tail-call that also is a recursive call. org or mail your article to review-team@geeksforgeeks. 1Review: Iteration vs. So let us discuss briefly on time complexity and the behavior of Recursive v/s Iterative functions. The time complexity of recursion is higher than Iteration due to the overhead of maintaining the function call stack. Time Complexity: Intuition for Recursive Algorithm. Both recursion and iteration run a chunk of code until a stopping condition is reached. Recursion is when a statement in a function calls itself repeatedly. It is not the very best in terms of performance but more efficient traditionally than most other simple O (n^2) algorithms such as selection sort or bubble sort. e. Explanation: Since ‘mid’ is calculated for every iteration or recursion, we are diving the array into half and then try to solve the problem. The time complexity of recursion is higher than Iteration due to the overhead of maintaining the function call stack. This article presents a theory of recursion in thinking and language. We often come across this question - Whether to use Recursion or Iteration. There are O(N) recursive calls in our recursive approach, and each call uses O(1) operations. Performs better in solving problems based on tree structures. The complexity of this code is O(n). Yes. Its time complexity is fairly easier to calculate by calculating the number of times the loop body gets executed. When considering algorithms, we mainly consider time complexity and space complexity. Using a simple for loop to display the numbers from one. It causes a stack overflow because the amount of stack space allocated to each process is limited and far lesser than the amount of heap space allocated to it. Sometimes the rewrite is quite simple and straight-forward. 3: An algorithm to compute mn of a 2x2 matrix mrecursively using repeated squaring. A recursive implementation and an iterative implementation do the same exact job, but the way they do the job is different. Recursion 可能會導致系統 stack overflow. )) chooses the smallest of. In the logic of computability, a function maps one or more sets to another, and it can have a recursive definition that is semi-circular, i. If it is, the we are successful and return the index. Iteration terminates when the condition in the loop fails. 1. Recursive algorithm's time complexity can be better estimated by drawing recursion tree, In this case the recurrence relation for drawing recursion tree would be T(n)=T(n-1)+T(n-2)+O(1) note that each step takes O(1) meaning constant time,since it does only one comparison to check value of n in if block. Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. Iteration is quick in comparison to recursion. At any given time, there's only one copy of the input, so space complexity is O(N). It is slower than iteration. Recursion is not intrinsically better or worse than loops - each has advantages and disadvantages, and those even depend on the programming language (and implementation). It is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. e. Time Complexity : O(2^N) This is same as recursive approach because the basic idea and logic is same. Euclid’s Algorithm: It is an efficient method for finding the GCD (Greatest Common Divisor) of two integers. Iteration is a sequential, and at the same time is easier to debug. We can define factorial in two different ways: 5. Major difference in time/space complexity between code running recursion vs iteration is caused by this : as recursion runs it will create new stack frame for each recursive invocation. Scenario 2: Applying recursion for a list. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are. Let's abstract and see how to do it in general. e. Once you have the recursive tree: Complexity. 2. T ( n ) = aT ( n /b) + f ( n ). Recursion often result in relatively short code, but use more memory when running (because all call levels accumulate on the stack) Iteration is when the same code is executed multiple times, with changed values of some variables, maybe better approximations or whatever else. Auxiliary Space: O(n), The extra space is used due to the recursion call stack. If time complexity is the point of focus, and number of recursive calls would be large, it is better to use iteration. In this video, I will show you how to visualize and understand the time complexity of recursive fibonacci. the use of either of the two depends on the problem and its complexity, performance. Improve this. Then we notice that: factorial(0) is only comparison (1 unit of time) factorial(n) is 1 comparison, 1 multiplication, 1 subtraction and time for factorial(n-1) factorial(n): if n is 0 return 1 return n * factorial(n-1) From the above analysis we can write:DFS. Because each call of the function creates two more calls, the time complexity is O(2^n); even if we don’t store any value, the call stack makes the space complexity O(n). Additionally, I'm curious if there are any advantages to using recursion over an iterative approach in scenarios like this. Transforming recursion into iteration eliminates the use of stack frames during program execution. The Space Complexity is O(N) and the Time complexity is O(2^N) because the root node has 2 children and 4 grandchildren. Functional languages tend to encourage recursion. Thus fib(5) will be calculated instantly but fib(40) will show up after a slight delay. An example of using the findR function is shown below. Though average and worst-case time complexity of both recursive and iterative quicksorts are O(N log N) average case and O(n^2). Iterative Backtracking vs Recursive Backtracking; Time and Space Complexity; Introduction to Iteration. Auxiliary Space: DP may have higher space complexity due to the need to store results in a table. With this article at OpenGenus, you must have the complete idea of Tower Of Hanoi along with its implementation and Time and Space. Recursion vs. We have discussed iterative program to generate all subarrays. , opposite to the end from which the search has started in the list. So, if you’re unsure whether to take things recursive or iterative, then this section will help you make the right decision. The first function executes the ( O (1) complexity) statements in the while loop for every value between a larger n and 2, for an overall complexity of O (n). There is more memory required in the case of recursion. When deciding whether to. Note: To prevent integer overflow we use M=L+(H-L)/2, formula to calculate the middle element, instead M=(H+L)/2. Next, we check to see if number is found in array [index] in line 4. fib(n) grows large. 2. Iteration is quick in comparison to recursion. We can optimize the above function by computing the solution of the subproblem once only. Recursive implementation uses O (h) memory (where h is the depth of the tree). 4. Here are the general steps to analyze loops for complexity analysis: Determine the number of iterations of the loop. Recursion produces repeated computation by calling the same function recursively, on a simpler or smaller subproblem. n in this example is the quantity of Person s in personList. 1 Predefined List Loops. Often writing recursive functions is more natural than writing iterative functions, especially for a rst draft of a problem implementation. g. What is the average case time complexity of binary search using recursion? a) O(nlogn) b) O(logn) c) O(n) d) O(n 2). In maths, one would write x n = x * x n-1. While tail-recursive calls are usually faster for list reductions—like the example we’ve seen before—body-recursive functions can be faster in some situations. We can see that return mylist[first] happens exactly once for each element of the input array, so happens exactly N times overall. Practice. Complexity Analysis of Ternary Search: Time Complexity: Worst case: O(log 3 N) Average case: Θ(log 3 N) Best case: Ω(1) Auxiliary Space: O(1) Binary search Vs Ternary Search: The time complexity of the binary search is less than the ternary search as the number of comparisons in ternary search is much more than binary search. Iteration produces repeated computation using for loops or while. So the worst-case complexity is O(N). When a function is called recursively the state of the calling function has to be stored in the stack and the control is passed to the called function. The graphs compare the time and space (memory) complexity of the two methods and the trees show which elements are calculated. Instead of many repeated recursive calls we can save the results, already obtained by previous steps of algorithm. Thus, the time complexity of factorial using recursion is O(N). A single point of comparison has a bias towards the use-case of recursion and iteration, in this case; Iteration is much faster. io. Time Complexity calculation of iterative programs. Secondly, our loop performs one assignment per iteration and executes (n-1)-2 times, costing a total of O(n. Where have I gone wrong and why is recursion different from iteration when analyzing for Big-O? recursion; iteration; big-o; computer-science; Share. It keeps producing smaller versions at each call. In this case, our most costly operation is assignment. The time complexity in iteration is. For some examples, see C++ Seasoning for the imperative case. 2 Answers. T (n) = θ. Big O notation mathematically describes the complexity of an algorithm in terms of time and space. Iteration and recursion are key Computer Science techniques used in creating algorithms and developing software. Generally, it has lower time complexity. However, if you can set up tail recursion, the compiler will almost certainly compile it into iteration, or into something which is similar, giving you the readability advantage of recursion, with the performance. In the recursive implementation on the right, the base case is n = 0, where we compute and return the result immediately: 0! is defined to be 1. What we lose in readability, we gain in performance. In more formal way: If there is a recursive algorithm with space. The total time complexity is then O(M(lgmax(m1))). Recursion: Recursion has the overhead of repeated function calls, that is due to the repetitive calling of the same function, the time complexity of the code increases manyfold. fib(n) is a Fibonacci function. Iterative Backtracking vs Recursive Backtracking; Time and Space Complexity; Introduction to Iteration. geeksforgeeks. Understand Iteration and Recursion Through a Simple Example In terms of time complexity and memory constraints, iteration is preferred over recursion. Answer: In general, recursion is slow, exhausting computer’s memory resources while iteration performs on the same variables and so is efficient. The recursive step is n > 0, where we compute the result with the help of a recursive call to obtain (n-1)!, then complete the computation by multiplying by n. So it was seen that in case of loop the Space Complexity is O(1) so it was better to write code in loop instead of tail recursion in terms of Space Complexity which is more efficient than tail. Here is where lower bound theory works and gives the optimum algorithm’s complexity as O(n). Firstly, our assignments of F[0] and F[1] cost O(1) each. quicksort, merge sort, insertion sort, radix sort, shell sort, or bubble sort, here is a nice slide you can print and use:The Iteration Method, is also known as the Iterative Method, Backwards Substitution, Substitution Method, and Iterative Substitution. Recursion is quite slower than iteration. Focusing on space complexity, the iterative approach is more efficient since we are allocating a constant amount O(1) of space for the function call and. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. This is the main part of all memoization algorithms. g. , it runs in O(n). In dynamic programming, we find solutions for subproblems before building solutions for larger subproblems. Sorted by: 1. From the package docs : big_O is a Python module to estimate the time complexity of Python code from its execution time. Iteration terminates when the condition in the loop fails. The recursive version can blow the stack in most language if the depth times the frame size is larger than the stack space. Because of this, factorial utilizing recursion has. There are factors ignored, like the overhead of function calls. However -these are constant number of ops, while not changing the number of "iterations". In Java, there is one situation where a recursive solution is better than a. Iteration: Iteration is repetition of a block of code. Both algorithms search graphs and have numerous applications. To visualize the execution of a recursive function, it is. In terms of (asymptotic) time complexity - they are both the same. 11. Backtracking. mov loopcounter,i dowork:/do work dec loopcounter jmp_if_not_zero dowork. Using iterative solution, no extra space is needed. . Because of this, factorial utilizing recursion has an O time complexity (N). Therefore Iteration is more efficient. Analysis of the recursive Fibonacci program: We know that the recursive equation for Fibonacci is = + +. (By the way, we can observe that f(a, b) = b - 3*a and arrive at a constant-time implementation. of times to find the nth Fibonacci number nothing more or less, hence time complexity is O(N), and space is constant as we use only three variables to store the last 2 Fibonacci numbers to find the next and so on. O (n) or O (lg (n)) space) to execute, while an iterative process takes O (1) (constant) space. Clearly this means the time Complexity is O(N). 3: An algorithm to compute mn of a 2x2 matrix mrecursively using repeated squaring. To my understanding, the recursive and iterative version differ only in the usage of the stack. Recursion. Strengths and Weaknesses of Recursion and Iteration. However, just as one can talk about time complexity, one can also talk about space complexity. Explaining a bit: we know that any computable. Time Complexity Analysis. Removing recursion decreases the time complexity of recursion due to recalculating the same values. Iteration & Recursion. The major driving factor for choosing recursion over an iterative approach is the complexity (i. Where branches are the number of recursive calls made in the function definition and depth is the value passed to the first call. Time complexity is relatively on the lower side. Its time complexity is easier to calculate by calculating the number of times the loop body gets executed. For Example, the Worst Case Running Time T (n) of the MERGE SORT Procedures is described by the recurrence. the last step of the function is a call to the. iterations, layers, nodes in each layer, training examples, and maybe more factors. So the best case complexity is O(1) Worst Case: In the worst case, the key might be present at the last index i. What is the time complexity to train this NN using back-propagation? I have a basic idea about how they find the time complexity of algorithms, but here there are 4 different factors to consider here i. Radix Sort is a stable sorting algorithm with a general time complexity of O (k · (b + n)), where k is the maximum length of the elements to sort ("key length"), and b is the base. –In order to find their complexity, we need to: •Express the ╩running time╪ of the algorithm as a recurrence formula. In terms of time complexity and memory constraints, iteration is preferred over recursion. personally, I find it much harder to debug typical "procedural" code, there is a lot of book keeping going on as the evolution of all the variables has to be kept in mind. When recursion reaches its end all those frames will start. Standard Problems on Recursion. Time Complexity: O(N) Space Complexity: O(1) Explanation. Iterative vs recursive factorial. Identify a pattern in the sequence of terms, if any, and simplify the recurrence relation to obtain a closed-form expression for the number of operations performed by the algorithm. I just use a normal start_time = time. It consists of initialization, comparison, statement execution within the iteration, and updating the control variable. Since you cannot iterate a tree without using a recursive process both of your examples are recursive processes. Analysis. Reduced problem complexity Recursion solves complex problems by. When recursion reaches its end all those frames will start unwinding. Standard Problems on Recursion. Things get way more complex when there are multiple recursive calls. There is less memory required in the case of iteration Send. Backtracking at every step eliminates those choices that cannot give us the. Stack Overflowjesyspa • 9 yr. As can be seen, subtrees that correspond to subproblems that have already been solved are pruned from this recursive call tree. In this tutorial, we’ll talk about two search algorithms: Depth-First Search and Iterative Deepening. And the space complexity of iterative BFS is O (|V|). The difference may be small when applied correctly for a sufficiently complex problem, but it's still more expensive. It talks about linear recursive processes, iterative recursive processes (like the efficient recursive fibr), and tree recursion (the naive inefficient fib uses tree recursion). Iteration is preferred for loops, while recursion is used for functions. Recursion often result in relatively short code, but use more memory when running (because all call levels accumulate on the stack) Iteration is when the same code is executed multiple times, with changed values of some variables, maybe better approximations or whatever else. Selection Sort Algorithm – Iterative & Recursive | C, Java, Python. The reason that loops are faster than recursion is easy. Thus, the time complexity of factorial using recursion is O(N). Fibonacci Series- Recursive Method C++ In general, recursion is best used for problems with a recursive structure, where a problem can be broken down into smaller versions. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. Photo by Compare Fibre on Unsplash. Some say that recursive code is more "compact" and simpler to understand. As for the recursive solution, the time complexity is the number of nodes in the recursive call tree.