Problem 1:
操作给定的二叉树,将其变换为源二叉树的镜像.
输入描述:
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
Recursion:
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public void Mirror(TreeNode root) {
if(root == null)
return;
if(root.left == null && root.right == null)
return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
if(root.left != null)
Mirror(root.left);
if(root.right != null)
Mirror(root.right);
return;
}
}
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
public void Mirror(TreeNode root) {
if(root == null)
return;
Queue<TreeNode> q = new LinkedList<>(); //BFS
q.offer(root);
TreeNode cur, temp;
while(!q.isEmpty())
{
int len = q.size();
for(int i = 0; i < len; i++)
{
cur = q.poll();
temp = cur.left;
cur.left = cur.right;
cur.right = temp;
if(cur.right != null) q.offer(cur.right);
if(cur.left != null) q.offer(cur.left);
}
}
return;
}
}
Problem 2:
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> ans = new ArrayList<>();
if(matrix.length == 0 || matrix[0].length == 0)
return ans;
int i = 0;
int top = 0;
int left = 0;
int bottom = matrix.length - 1;
int right = matrix[0].length - 1;
while(true)
{
for(i = left; i <= right; i++) //top
ans.add(matrix[top][i]);
top++;
if(top > bottom)
break;
for(i = top; i <= bottom; i++) //right
ans.add(matrix[i][right]);
right--;
if(left > right)
break;
for(i = right; i >= left; i--) //bottom
ans.add(matrix[bottom][i]);
bottom--;
if(top > bottom)
break;
for(i = bottom; i >= top; i--) //left
ans.add(matrix[i][left]);
left++;
if(left > right)
break;
}
return ans;
}
}
Problem 3:
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
注意:保证测试中不会当栈为空的时候,对栈调用pop()或者min()或者top()方法。
Double stacks:
import java.util.Stack;
public class Solution {
Stack<Integer> stackTotal = new Stack<Integer>();
Stack<Integer> stackLittle = new Stack<Integer>();
public void push(int node) {
stackTotal.push(node);
if(stackLittle.empty()){
stackLittle.push(node);
}else{
if(node <= stackLittle.peek()){
stackLittle.push(node);
}else{
stackLittle.push(stackLittle.peek());
}
}
}
public void pop() {
stackTotal.pop();
stackLittle.pop();
}
public int top() {
return stackTotal.peek();
}
public int min() {
return stackLittle.peek();
}
}
One stack:
import java.util.Stack;
public class Solution {
Stack<Integer> s = new Stack<Integer>();
Integer minv = Integer.MAX_VALUE;
public void push(int node) {
if(s.empty()){
s.push(node);
minv = node;
}
else{
if(node <= minv){
s.push(minv); store the minimum of the items that have been pushed in the stack
minv = node;
}
s.push(node);
}
}
public void pop() {
if(minv == s.peek()){
if(s.size() >1){
s.pop();
minv = s.peek(); //update minimum
}
}
s.pop();
}
public int top() {
return s.peek();
}
public int min() {
return minv;
}
}
Problem 4:
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0 || pushA.length != popA.length)
return false;
Stack<Integer> s = new Stack<>();
int j = 0;
for(int i = 0; i < pushA.length; i++){
s.push(pushA[i]);
while(!s.isEmpty() && s.peek() == popA[j]){
s.pop();
j++;
}
}
return s.isEmpty();
}
}
Problem 5:
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
if(root == null)
return ans;
q.offer(root);
TreeNode cur;
while(!q.isEmpty()){
cur = q.poll();
ans.add(cur.val);
if(cur.left != null) q.offer(cur.left);
if(cur.right != null) q.offer(cur.right);
}
return ans;
}
}