共同祖先
作者:互联网
共同祖先
1
2 3
4 5 6 7
N N 8 9 N N 10 11
- 输入:int[] nums={1,2,3,4,5,6,7,N,N,8,9,N,N,10,11};
- 代码:
class Ancestors
{
public class Trees
{
public int NodeTree { get; set; }//根节点
public int LeftTree { get; set; } //左节点
public int RightTree { get; set; }//右节点
public int Hierarchy { get; set; }//层级
}
public void MainAncestors()
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7, -2, -2, 8, 9, -2, -2, 10, 11 };
List<Trees> listTree = new List<Trees>();
listTree.Add(new Trees
{
NodeTree = 1,
LeftTree = 2,
RightTree = 3,
Hierarchy = 0
});
listTree.Add(new Trees
{
NodeTree = 2,
LeftTree = 4,
RightTree = 5,
Hierarchy = 1
}) ;
listTree.Add(new Trees
{
NodeTree = 3,
LeftTree = 6,
RightTree = 7,
Hierarchy = 1
});
listTree.Add(new Trees
{
NodeTree = 4,
LeftTree = -2,
RightTree = -2,
Hierarchy = 2
});
listTree.Add(new Trees
{
NodeTree = 5,
LeftTree = 8,
RightTree = 9,
Hierarchy = 2
});
listTree.Add(new Trees
{
NodeTree = 6,
LeftTree = -2,
RightTree = -2,
Hierarchy = 2
});
listTree.Add(new Trees
{
NodeTree = 7,
LeftTree = 10,
RightTree = 11,
Hierarchy = 2
});
Trees LastTree = new Trees {
NodeTree = -1,
LeftTree = -1,
RightTree = -1,
Hierarchy = -1
};
List<Trees> LtreesValue = new List<Trees>();//左节点所涉及的集合
List<Trees> RtreesValue = new List<Trees>();//右节点所涉及的集合
int nums1 = Convert.ToInt32(Console.ReadLine());
int nums2 = Convert.ToInt32(Console.ReadLine());
DownLeftTrees(nums1, listTree, ref LtreesValue);//从上至下
UpLeftTrees(nums1, listTree, ref LtreesValue);//从下至上(所涉及nums1的全部节点)
DownLeftTrees(nums2, listTree, ref RtreesValue);
UpLeftTrees(nums2, listTree, ref RtreesValue);//所涉及nums2的全部节点
for (int i=0;i< LtreesValue.Count; i++)
{
for (int j = 0; j < RtreesValue.Count; j++)
{
if (LtreesValue[i] == RtreesValue[j])
{
if (LastTree.Hierarchy < RtreesValue[j].Hierarchy)
{
LastTree = RtreesValue[j];
}
}
}
}
Console.WriteLine(LastTree.NodeTree);
}
private void UpLeftTrees(int leftnums, List<Trees> listTree, ref List<Trees> LtreesValue)
{
for (int i = 0; i < listTree.Count; i++)
{
int lefttree = listTree[i].LeftTree;
int nodetrees = listTree[i].NodeTree;
int righttree = listTree[i].RightTree;
if (lefttree == leftnums || righttree == leftnums|| nodetrees == leftnums)
//如果上层树中包含leftnums,LtreesValue++
{
if (!LtreesValue.Contains(listTree[i]))
{
LtreesValue.Add(listTree[i]);
UpLeftTrees(nodetrees , listTree, ref LtreesValue);
}
}
}
}
private void DownLeftTrees(int leftnums, List<Trees> listTree, ref List<Trees> LtreesValue)
{
for (int i = 0; i < listTree.Count; i++)
{
int lefttree = listTree[i].LeftTree;
int nodetrees = listTree[i].NodeTree;
int righttree = listTree[i].RightTree;
if (nodetrees == leftnums)// 如果下层树中包含leftnums,LtreesValue++
{
if (!LtreesValue.Contains(listTree[i]))
{
LtreesValue.Add(listTree[i]);
DownLeftTrees(lefttree, listTree, ref LtreesValue);//左数
}
if (!LtreesValue.Contains(listTree[i]))
{
LtreesValue.Add(listTree[i]);
DownLeftTrees(righttsree, listTree, ref LtreesValue);//右数
}
}
}
}
}
标签:RightTree,LtreesValue,祖先,NodeTree,int,listTree,new,共同 来源: https://blog.csdn.net/m0_46446454/article/details/113844872