2

What am I missing in GetLowestLevelFoo? Why do I get the answer A instead of D?

    public class Foo
    {
        public string Name { get; set; }
        public Foo ChildFoo { get; set; }
    }

    [TestFixture]
    public class Recursion
    {
        [Test]
        public void Test()
        {
            Foo foo = new Foo
            {
                Name = "A",
                ChildFoo = new Foo
                {
                    Name = "B",
                    ChildFoo = new Foo
                    {
                        Name = "C",
                        ChildFoo = new Foo
                        {
                            Name = "D"
                        }
                    }
                }
            };

            Assert.AreEqual("D", GetLowestLevelFoo(foo).Name);
        }

        public Foo GetLowestLevelFoo(Foo foo)
        {
            if (foo.ChildFoo != null)
            {
                GetLowestLevelFoo(foo.ChildFoo);
            }
            return foo;
        }
    }

4 Answers 4

11

You only want to return foo when you'r at the lowest level. You were returning it no matter what. If you're not at the lowest level you should return the value returned from your recursive call.

public Foo GetLowestLevelFoo(Foo foo)
    {
        if (foo.ChildFoo != null)
        {
            return GetLowestLevelFoo(foo.ChildFoo);
        }
        else
        {
            return foo;
        }
    }
0
2

edited:

public Foo GetLowestLevelFoo(Foo foo)
{
    if (foo.ChildFoo != null)
    {
        return GetLowestLevelFoo(foo.ChildFoo);
    }
    return foo;
}
4
  • 1
    You need to return something within the IF as well.
    – Seth Moore
    Mar 3, 2010 at 18:02
  • my bad. quick typing during the mad-dash to supply an answer.
    – Bryan Rowe
    Mar 3, 2010 at 18:04
  • I understand. It's ruthless trying to answer questions out here.
    – Seth Moore
    Mar 3, 2010 at 18:35
  • You don't need an else. In fact to remove a level of indentation I would suggest NOT having the else. If the if statement is not satisfied it will skip it and go straight to the return statement - the same as falling into an else block. Mar 5, 2010 at 3:45
1

you need to assign foo with the result of your call to GetLowestLevelFoo.

foo = GetLowestLevelFoo(foo.ChildFoo)

otherwise you are returning what you started with.

0

As other have now commented, your code recurses back through the stack, returning each "level of "foo" : finally returning the topmost "node."

Try this :

public Foo GetLowestLevelFoo(Foo foo)
{
    if (foo.ChildFoo == null) return foo;

    return GetLowestLevelFoo(foo.ChildFoo);
}
1
  • @David B You are absolutely right. I will edit my response. I actually single-stepped through the OP's code and watched it return 'foo in four flavours, as it "crawled" back up the stack, before posting my response, so "double shame" on me :) Thanks !
    – BillW
    Mar 5, 2010 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.