文章目录
- 1.k个一组翻转链表(*)
- 2.随机链表的复制(*)
- 3.排序链表
- 4.合并k个升序链表(*)
- 5.LRU缓存(跳过)
- 6.二叉树的中序遍历
- 7.二叉树的最大深度
- 8.翻转二叉树
- 9.对称二叉树(*)
- 10.二叉树的直径
1.k个一组翻转链表(*)
定义start,end指针分别指向翻转的开始节点和结束节点
class Solution {public ListNode reverseKGroup(ListNode head, int k) {ListNode dummy = new ListNode();dummy.next = head;ListNode start = dummy;ListNode end = dummy;while (true) {for (int i = 0; i < k && end != null; i++) {end = end.next;}if (end == null) break;ListNode nextStart = end.next;end.next = null; ListNode first = start.next; start.next = reverse(start.next);first.next = nextStart; start = first;end = first;}return dummy.next;}public ListNode reverse(ListNode head) {ListNode dummy = null;while (head != null) {ListNode temp = head.next;head.next = dummy;dummy = head;head = temp;}return dummy;}
}
2.随机链表的复制(*)
考察hashmap与链表的结合使用
1.key为原始的节点,val为新节点
2.根据map设置节点的next和random值
class Solution {public Node copyRandomList(Node head) {HashMap<Node, Node> map = new HashMap<>();Node cur = head;while (cur != null) {Node newNode = new Node(cur.val);map.put(cur, newNode);cur = cur.next;}cur = head;while (cur != null) {Node newNode = map.get(cur);newNode.next = map.get(cur.next);newNode.random = map.get(cur.random);cur = cur.next;}return map.get(head);}
}
3.排序链表
递归分割链表+排序两个有序链表
class Solution {public ListNode sortList(ListNode head) {if (head == null || head.next == null) {return head;}ListNode slow = head;ListNode fast = head.next;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;}ListNode second = slow.next;slow.next = null;head = sortList(head);second = sortList(second);return merge(head, second);}public ListNode merge(ListNode list1, ListNode list2) {ListNode dummy = new ListNode();ListNode dummy_head = dummy;while (list1 != null && list2 != null) {if (list1.val < list2.val) {dummy.next = list1;list1 = list1.next;} else {dummy.next = list2;list2 = list2.next;}dummy = dummy.next;}if (list1 != null) dummy.next = list1;if (list2 != null) dummy.next = list2;return dummy_head.next;}}
4.合并k个升序链表(*)
两两合并,注意边界判断
for循环i+=2,一次处理两个节点
class Solution {public ListNode mergeKLists(ListNode[] lists) {if (lists.length <= 0) {return null;}List<ListNode> list = Arrays.asList(lists);while (list.size() > 1) {List<ListNode> temp = new ArrayList<>();for (int i = 0; i < list.size(); i+=2) {ListNode list1 = list.get(i);ListNode list2 = null;if (i+1 < list.size()) {list2 = list.get(i+1);}temp.add(merge(list1, list2));}list = temp;}return list.get(0);}public ListNode merge(ListNode list1, ListNode list2) {ListNode dummy = new ListNode();ListNode dummy_head = dummy;while (list1 != null && list2 != null) {if (list1.val < list2.val) {dummy.next = list1;list1 = list1.next;} else {dummy.next = list2;list2 = list2.next;}dummy = dummy.next;}if (list1 != null) dummy.next = list1;if (list2 != null) dummy.next = list2;return dummy_head.next;}
}
5.LRU缓存(跳过)
6.二叉树的中序遍历
考察中序遍历,外部定义一个数组即可
class Solution {List<Integer> res = new ArrayList<>();public List<Integer> inorderTraversal(TreeNode root) {if (root == null) return res;inorderTraversal(root.left);res.add(root.val);inorderTraversal(root.right);return res;}
}
7.二叉树的最大深度
函数的定义就是求函数的最大深度,相信定义
class Solution {public int maxDepth(TreeNode root) {if (root == null) return 0;int leftDepth = maxDepth(root.left);int rightDepth = maxDepth(root.right);return Math.max(leftDepth, rightDepth) + 1;}
}
8.翻转二叉树
先处理根节点,再递归处理左右节点
注意处理左右节点不需要返回值
前序实现
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) return null;TreeNode temp = root.left;root.left = root.right;root.right = temp;invertTree(root.left);invertTree(root.right);return root;}
}
9.对称二叉树(*)
左右两个节点进行判断,子节点同理递归实现
class Solution {public boolean isSymmetric(TreeNode root) {return fun(root.left, root.right);}public boolean fun(TreeNode left, TreeNode right) {if (left != null && right == null) return false;if (left == null && right != null) return false;if (left == null && right == null) return true;if (left.val != right.val) return false;return fun(left.left, right.right) && fun(left.right, right.left);}
}
10.二叉树的直径
必须定义另一个函数,方便可以返回maxRes
和二叉树的最大深度类似
class Solution {public int maxRes = 0;public int diameterOfBinaryTree(TreeNode root) {dfs(root);return maxRes;}public int dfs(TreeNode root) {if (root == null) return 0;int leftDepth = dfs(root.left);int rightDepth = dfs(root.right);maxRes = Math.max(leftDepth+rightDepth, maxRes);return Math.max(leftDepth, rightDepth) + 1;}
}