题目一
283. 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
说明:
必须在原数组上操作,不能拷贝额外的数组。
尽量减少操作次数。
我的答案
class Solution {
public void moveZeroes(int[] nums) {
int zero = 0; // 用于0计数
for(int i=nums.length-1; i>=0; i--){
if(nums[i] == 0){
// 这时才往后移
for(int j=i; j<nums.length-zero-1; j++){
// -zero 表示只需要移到后面已有的0前一位就行,不需要0和0交换位置,
// -1是因为只要当前位与后一位交换
nums[j] = nums[j+1];
nums[j+1] = 0;
}
zero++; // 表示0已经移到后面一次了
}
}
}
}
方法一:双指针
class Solution {
public void moveZeroes(int[] nums) {
int n = nums.length, left = 0, right = 0;
while (right < n) {
if (nums[right] != 0) {
swap(nums, left, right);
left++;
}
right++;
}
}
public void swap(int[] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
复杂度分析
- 时间复杂度:O(n),其中 n为序列长度。每个位置至多被遍历两次。
- 空间复杂度:O(1)。只需要常数的空间存放若干变量。
题目二
116. 填充每个节点的下一个右侧节点指针
给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
示例:
输入:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","left":{"id”:“2”,“left”:{“KaTeX parse error: Expected 'EOF', got '}' at position 53: …t":null,"val":4}̲,"next":null,"r…id”:“4”,“left”:null,“next”:null,“right”:null,“val”:5},“val”:2},“next”:null,“right”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"5","left":{"id”:“6”,“left”:null,“next”:null,“right”:null,“val”:6},“next”:null,“right”:{"$id":“7”,“left”:null,“next”:null,“right”:null,“val”:7},“val”:3},“val”:1}
输出:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","left":{"id”:“2”,“left”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“4”,“left”:null,“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“6”,“left”:null,“next”:null,“right”:null,“val”:7},“right”:null,“val”:6},“right”:null,“val”:5},“right”:null,“val”:4},“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"7","left":{"ref”:“5”},“next”:null,“right”:{“KaTeX parse error: Expected 'EOF', got '}' at position 9: ref":"6"}̲,"val":3},"righ…ref”:“4”},“val”:2},“next”:null,“right”:{"$ref":“7”},“val”:1}
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。
提示:
你只能使用常量级额外空间。
使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
我的答案
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if (root == null) return null;
// 层序遍历 广度优先遍历
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root); // 先将根节点添加进队列
while(!queue.isEmpty()){
List<Node> list = new ArrayList<>();
int queueSize = queue.size(); // 记录此时的队列的元素个数
for (int i = 0; i < queueSize; i++) {
// 取出队列中的节点,放进list中
Node node = queue.poll();
list.add(node);
// 如果node的左右节点存在就放进队列,即为下一层节点
if(node.left != null) queue.offer(node.left); // 先左再右
if(node.right != null) queue.offer(node.right);
}
// 现在list有每一层的节点,这时候从左往右添加next就行
for (int i = 0; i < list.size(); i++) {
System.out.print(""+list.get(i).val+", ");
if(i+1 == list.size()) list.get(i).next = null;
else list.get(i).next = list.get(i+1);
}
System.out.println("-----");
}
return root;
}
}
层次遍历
class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
// 初始化队列同时将第一层节点加入队列中,即根节点
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
// 外层的 while 循环迭代的是层数
while (!queue.isEmpty()) {
// 记录当前队列大小
int size = queue.size();
// 遍历这一层的所有节点
for (int i = 0; i < size; i++) {
// 从队首取出元素
Node node = queue.poll();
// 连接
if (i < size - 1) {
node.next = queue.peek();
}
// 拓展下一层节点
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
// 返回根节点
return root;
}
}
复杂度分析
- 时间复杂度:O(N)。每个节点会被访问一次且只会被访问一次,即从队列中弹出,并建立 next 指针。
- 空间复杂度:O(N)。这是一棵完美二叉树,它的最后一个层级包含 N/2 个节点。广度优先遍历的复杂度取决于一个层级上的最大元素数量。这种情况下空间复杂度为 O(N)。
方法二:使用已建立的 next 指针
class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
// 从根节点开始
Node leftmost = root;
while (leftmost.left != null) {
// 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针
Node head = leftmost;
while (head != null) {
// CONNECTION 1
head.left.next = head.right;
// CONNECTION 2
if (head.next != null) {
head.right.next = head.next.left;
}
// 指针向后移动
head = head.next;
}
// 去下一层的最左的节点
leftmost = leftmost.left;
}
return root;
}
}
复杂度分析
- 时间复杂度:O(N),每个节点只访问一次。
- 空间复杂度:O(1),不需要存储额外的节点。
题目三
剑指 Offer 27. 二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
4
/ \
2 7
/ \ / \
1 3 6 9
镜像输出:
4
/ \
7 2
/ \ / \
9 6 3 1
示例 1:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
限制:
0 <= 节点个数 <= 1000
我的答案
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null) return root;
TreeNode node = new TreeNode();
pro(root, node);
return node;
}
public void pro(TreeNode root, TreeNode node){
if(root != null){
node.val = root.val;
if(root.left != null){
node.right = new TreeNode();
pro(root.left, node.right);
}
if(root.right != null){
node.left = new TreeNode();
pro(root.right, node.left);
}
}
}
}
方法一:递归法
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null) return null;
TreeNode tmp = root.left;
root.left = mirrorTree(root.right);
root.right = mirrorTree(tmp);
return root;
}
}
复杂度分析:
时间复杂度 O(N) : 其中 N 为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用 O(N) 时间。
空间复杂度 O(N): 最差情况下(当二叉树退化为链表),递归时系统需使用 O(N)大小的栈空间。
方法二:辅助栈(或队列)
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null) return null;
Stack<TreeNode> stack = new Stack<>() {{ add(root); }};
while(!stack.isEmpty()) {
TreeNode node = stack.pop();
if(node.left != null) stack.add(node.left);
if(node.right != null) stack.add(node.right);
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
return root;
}
}
复杂度分析:
时间复杂度 O(N): 其中 N为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用 O(N) 时间。
空间复杂度 O(N) : 最差情况下(当为满二叉树时),栈 stackstack 最多同时存储 N/2 个节点,占用 O(N) 额外空间。