前序递归遍历找最大值:
//前序遍历找最大值 The preceding traversal finds the maximum value
public int FormerSequenceFindmax(Node head){
head = head.next;
if (head.next != null){
//如果head后面不为空,则将该值的data与后者的dada比较,并保留较大的值赋值给后者
//If the head is not empty, the data of that value is compared with the DADA of the latter, and the larger value is reserved for the latter
if(head.data > head.next.data){
head.next.data = head.data;
}
//向后移动 move backwards
return FormerSequenceFindmax(head);
}else{
return head.data;
}
}
后序递归遍历找最大值:
//后续遍历找最大值 Then iterate to find the maximum value
public int SubsequentTraversalFindmax(Node head){
head = head.next;
if (head.next != null){
//如果head后面不为空,向后移动 If head is not empty behind, move backwards
SubsequentTraversalFindmax(head);
//两个之间进行比较,保留较大的值并赋值给前者
//Compare between the two, reserving the larger value and assigning it to the former
if(head.data < head.next.data){
head.data = head.next.data;
}
return head.data;
}else{
return head.data;
}
}
本文介绍了一种使用递归方法在链表中寻找最大值的技术,包括前序遍历和后序遍历两种方式。通过递归调用自身并比较节点值来更新最大值。
1373

被折叠的 条评论
为什么被折叠?



