C# 递归函数在无限循环中着陆。即使在返回语句求值之后,控件仍将返回到function调用
我需要返回C# 递归函数在无限循环中着陆。即使在返回语句求值之后,控件仍将返回到function调用,c#,C#,我需要返回UserID,当linq查询返回null值时,它是字符串格式的,但即使在else部分执行return语句之后,控件仍返回函数调用 public string GetLastLeafLeft(string Id) { var leftchildId = dbcontext .NMTrees .Where(ll => ll.UserID == Id) .Select(tt => tt
UserID
,当linq查询返回null
值时,它是字符串格式的,但即使在else
部分执行return
语句之后,控件仍返回函数调用
public string GetLastLeafLeft(string Id)
{
var leftchildId =
dbcontext
.NMTrees
.Where(ll => ll.UserID == Id)
.Select(tt => tt.LeftChildID)
.FirstOrDefault();
if (leftchildId != null)
{
return GetLastLeafLeft(leftchildId);
}
else
{
return leftchildId;
}
}
最后一个左子节点是没有左子节点的节点,但不是没有左子节点的节点的左子节点
if (leftchildId != null)
{
return GetLastLeafLeft(leftchildId);
}
else
{
return Id; // Node that does not have left child
}
我放弃了使用递归函数,我提出了一个使用while循环的解决方案
var LLeafParent = GetLastLeafLeft(ParentId);
while (LLeafParent != null) // To return last node of tree which has null left child
{
var res = GetLastLeafLeft(LLeafParent);
if (res == null)
break;
LLeafParent = res; // returns ID when it has a left node null
}
//返回左子对象的函数
public string GetLastLeafLeft(string Id)
{
var leftchildId = dbcontext.NMTrees.Where(ll => ll.UserID == Id).Select(tt => tt.LeftChildID).FirstOrDefault();
return leftchildId;
}
诊断问题:leaf LeftChildID引用是否在某个位置备份了链?例如,A引用B,引用A?你是想返回
leftchildId
(null
)还是Id
?如果这是一个任意长度的列表,你一定会得到一个stackoverflow
例外,你能给我们看一下NMTrees
的类型以及一些样本数据(C格式)?grant yes我正在尝试返回Id(字符串格式)它将子节点保留为空值/me耐心等待,以查看Manjunath是否会对此问题提供任何澄清,从而使问题重现。是的,我希望返回没有剩余子节点的节点。即左子代为空