- 递归 递归就是方法调用自身,写递归时注意两项条件:
- 反转单链表
A->B->C->D 反转后变为 D->C->B->A
public class SingleNodeList{
int item;
public SingleNodeList(int item) {
this.item = item;
}
SingleNodeList next;
public static SingleNodeList reverse(SingleNodeList node) {
if(node == null || node.next == null) {
return node;
}else{
SingleNodeList head = reverse(node.next);
node.next.next = node;
node.next = null;
return head;
}
}
}
- 求二叉树的高度、深度,前序、中序、后序排序
/**
*
* 描述:获取二叉树的高度和宽度
*
* @author yus
* @since 1.8
*/
public class TreeNode<U> {
public TreeNode<U> left;
public TreeNode<U> right;
public U item;
public TreeNode(U item) {
this.item = item;
}
@Override
public String toString() {
return "TreeNode [left=" + left + ", right=" + right + ", item=" + item + "]";
}
// 二叉树取最大深度
public static int getMaxDepth(TreeNode<String> root) {
if (root == null)
return 0;
else {
int left = getMaxDepth(root.left);
int right = getMaxDepth(root.right);
return 1 + Math.max(left, right);
}
}
// 获取二叉树的宽度
public static int getMaxWidth(TreeNode<String> root) {
if (root == null) {
return 0;
} else {
Deque<TreeNode<String>> queue = new ArrayDeque<TreeNode<String>>();
queue.add(root);
int maxLen = 1;
while (true) {
// 外层循环一次,二叉树向下查询一层
int len = queue.size();
if (len == 0) {
break;
}
while (len > 0) {
// 添加子节点,移除父节点
TreeNode<String> node = queue.poll();
len--;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
maxLen = Math.max(maxLen, queue.size());
}
return maxLen;
}
}
// 前序遍历
public static <U> void preOrder(TreeNode<U> root, List<TreeNode<U>> list) {
if (root != null) {
list.add(root);
preOrder(root.left, list);
preOrder(root.right, list);
}
}
// 中序遍历
public static <U> void midOrder(TreeNode<U> root, List<TreeNode<U>> list) {
if (root != null) {
midOrder(root.left, list);
list.add(root);
midOrder(root.right, list);
}
}
// 后序遍历
public static <U> void postOrder(TreeNode<U> root, List<TreeNode<U>> list) {
if (root != null) {
postOrder(root.left, list);
postOrder(root.right, list);
list.add(root);
}
}
//层次遍历
private static List<List<String>> result = new LinkedList<List<String>>();
public static List<List<String>> layOrder(TreeNode<String> node) {
insertList(node, 0);
return result;
}
private static void insertList (TreeNode<String> node, int level) {
if(node == null) {
return;
}
if(level < result.size()) {
result.get(level).add((String) node.item);
}else { //新建结果集
result.add(new LinkedList<String>());
result.get(level).add((String) node.item);
}
insertList(node.left, level+1);
insertList(node.right, level+1);
}
public static void main(String[] args) {
TreeNode<String> root = new TreeNode<String>("1");
TreeNode<String> left1 = new TreeNode<String>("2");
TreeNode<String> right1 = new TreeNode<String>("3");
TreeNode<String> left2 = new TreeNode<String>("4");
TreeNode<String> right2 = new TreeNode<String>("5");
root.left = left1;
root.right = right1;
left1.left = left2;
left1.right = right2;
int maxDepth = getMaxDepth(root);
System.out.println("depth: " + maxDepth);
int maxWidth = getMaxWidth(root);
System.out.println("width: " + maxWidth);
System.out.print("前序遍历: ");
List<TreeNode<String>> list = new ArrayList<TreeNode<String>>();
preOrder(root, list);
for (TreeNode<String> treeNode : list) {
System.out.print(treeNode.item + "\t");
}
System.out.println();
System.out.print("中序遍历: ");
list = new ArrayList<TreeNode<String>>();
midOrder(root, list);
for (TreeNode<String> treeNode : list) {
System.out.print(treeNode.item + "\t");
}
System.out.println();
System.out.print("后序遍历: ");
list = new ArrayList<TreeNode<String>>();
postOrder(root, list);
for (TreeNode<String> treeNode : list) {
System.out.print(treeNode.item + "\t");
}
System.out.println();
//层次遍历
List<List<String>> layOrderList = layOrder(root);
System.out.println(layOrderList);
}
}