1. 删除链表中的重复结点。
/**
* 输入:2, 3, 5, 8, 3, 7, 8, 9, 8, 9, 10
* 输出:2, 3, 5, 7, 8, 9, 10
*/
public class TestClass {
public static void main(String[] args) {
// 构造单链表
int[] num = { 2, 3, 5, 8, 3, 7, 8, 9, 8, 9, 10 };
ListNode head = new ListNode(num[0]);
ListNode temp = head;
for (int i = 1; i < num.length; i++) {
ListNode node = new ListNode(num[i]);
temp.next = node;
temp = node;
}
// 构造单链表结束
// 输出链表
printList(head);
// 链表排序
head = bubbleSort(head);
// 输出链表
printList(head);
// 删除重复结点
head = deleteDuplicate(head);
// 输出链表
printList(head);
}
private static ListNode bubbleSort(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode cur = null;
ListNode tail = null;
cur = head;
while(cur.next != tail){
while (cur.next != tail) {
if(cur.data > cur.next.data){
int temp = cur.data;
cur.data = cur.next.data;
cur.next.data = temp;
}
cur = cur.next;
}
tail = cur;
cur = head;
}
return head;
}
private static ListNode deleteDuplicate(ListNode head) {
ListNode cur = head;
if (cur == null) {
return head;
}
while (cur.next != null) {
if (cur.data == cur.next.data) {
cur.next = cur.next.next; // 当前值与下一个值相等, 则删除
} else {
cur = cur.next;
}
}
return head;
}
/**
* 遍历输出链表
* @param head 头结点
*/
private static void printList(ListNode head) {
ListNode temp = head;
if (temp == null) {
return;
}
temp.display();
while (temp.next != null) {
temp.next.display();
temp = temp.next;
}
System.out.println();
}
}
class ListNode {
int data;
ListNode next;
public ListNode(int data) {
this.data = data;
}
public ListNode getNext(){
return next;
}
public void display(){
System.out.print(data + ", ");
}
}
2. 返回二叉树中某结点的所有祖先结点
/**
* 返回二叉树中某结点的所有祖先结点
* 13
* / \
* 8 99
* / \ /
* 6 10 54
* \
* 7
*/
public class TestClass2 {
public static void main(String[] args) {
int[] content = new int[] { 13, 8, 6, 7, 10, 99, 54 };
BinaryTree tree = new BinaryTree(content);
tree.preOrder(tree.root);
System.out.println();
tree.printAncestors(tree.root, 7);
}
}
/**
* 二叉树结点
*
*/
class TreeNode {
int data;
TreeNode leftChild;
TreeNode rightChild;
public TreeNode(int data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
}
class BinaryTree {
TreeNode root;
public BinaryTree(int[] data) {
for (int i = 0; i < data.length; i++) {
addNode(data[i]);
}
}
private void addNode(int data) {
TreeNode current = root;
if (root == null) {
root = new TreeNode(data);
return;
}
while (true) {
if (data < current.data) {
if (current.leftChild == null) {
current.leftChild = new TreeNode(data);
return;
} else {
current = current.leftChild;
}
} else {
if (current.rightChild == null) {
current.rightChild = new TreeNode(data);
return;
} else {
current = current.rightChild;
}
}
}
}
public void preOrder(TreeNode node) {
if (node != null) {
System.out.print("[" + node.data + "] ");
preOrder(node.leftChild);
preOrder(node.rightChild);
}
}
public void floorOrder(TreeNode node){
if (node == null){
return;
}
LinkedList<TreeNode> list = new LinkedList<>();
list.add(root);
TreeNode current;
while (!list.isEmpty()) {
current = list.poll();
System.out.print(current.data + ", ");
if(current.leftChild != null){
list.add(current.leftChild);
}
if(current.rightChild != null){
list.add(current.rightChild);
}
}
}
public boolean printAncestors(TreeNode node, int value) {
if (node == null) {
return false;
}
if (node.data == value) {
return true;
}
if (printAncestors(node.leftChild, value) || printAncestors(node.rightChild, value)) {
System.out.print(node.data + " ");
return true;
}
return false;
}
}