What is the Big-O Time complexity of this recursive call with a for-loop

devguy :

Question, when analyzing the time complexity of the following code (never mind the space complexity). Would this be O(n)? the recursive call count as constant? I'm not sure because of the recursive call within the for loop.

public void printViews(View view) {
        if (view instanceof ViewGroup && ((ViewGroup)view).getChildCount() > 0) {

            ViewGroup viewGroup = ((ViewGroup)view);
            int childCount = viewGroup.getChildCount();

            for (int i = 0; i < childCount; i++) {
                Log.d("Test", viewGroup.getChildAt(i).getClass().getSimpleName());

                if (viewGroup.getChildAt(i) instanceof ViewGroup)
                    printViews(viewGroup.getChildAt(i));  //recursion
            }
        } else {
            Log.d("Test", view.getClass().getSimpleName());
        }
    }
S.A :

It is O(n).

What you are doing is DFS on the view tree. The recursive call is not constant but it is a way of traversing the tree.

see the link for more details: https://en.wikipedia.org/wiki/Depth-first_search

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=125868&siteId=1