6道题起步
水题狂练
树的子结构
题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null&&root2!=null) return false;
if(root2==null) return false;
return dfs(root1,root2)||dfs(root1.left,root2)||dfs(root1.right,root2);
}
public boolean dfs(TreeNode root1,TreeNode root2)
{
if(root2==null) return true;
if(root1==null&&root2==null)
return true;
else if(root1==null||root2==null)
return false;
if(root1.val!=root2.val)
{
return false;
}
boolean bL = dfs(root1.left,root2.left);
if(!bL) return false;
boolean bR = dfs(root1.right,root2.right);
if(!bR) return false;
return true;
}
}
下一题
镜像二叉树
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
::想不用递归都不行,这题想用栈就是给自己找麻烦
…尽量不在下一层递归里面判断条件,当前层能判断就当前层判断,尽量减枝,少开栈
public class Solution {
public void Mirror(TreeNode root) {
if(root==null)
return;
TreeNode l = root.left;
TreeNode r = root.right;
root.left = r;
root.right = l;
if(r!=null) Mirror(r);
if(l!=null) Mirror(l);
}
}
下下题
最小栈
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
这题是重点
import java.util.Stack;
public class Solution {
private Stack <Integer>stack;
private Stack<Integer> minStack;
private int curMin;
{
stack = new Stack();
minStack = new Stack();
curMin = Integer.MAX_VALUE;
}
public void push(int node) {
stack.push(node);
if(curMin>node){
curMin = node;
}
minStack.push(curMin);
}
public void pop() {
stack.pop();
minStack.pop();
curMin = minStack.peek();
}
public int top() {
return stack.peek();
}
public int min() {
return curMin;
}
}
栈的弹入弹出序列
题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
//要模拟多次入栈,肯定要一个双重循环
Stack<Integer> stack = new Stack();
int index=0;
for(int i=0;i<pushA.length;++i)
{
stack.push(pushA[i]);
while(!stack.isEmpty()&&stack.peek()==popA[index])
{
stack.pop();
index++;
}
}
return stack.isEmpty();
}
}
层序遍历二叉树
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印
import java.util.ArrayList;
/**
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 ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> ans = new ArrayList();
if(root==null) return ans;
Queue<TreeNode> q = new LinkedList();
q.offer(root);
while(!q.isEmpty()){
TreeNode top = q.poll();
ans.add(top.val);
if(top.left!=null) q.offer(top.left);
if(top.right!=null) q.offer(top.right);
}
return ans;
}
}
旋转打印矩阵
这是我大一的噩梦,不过现在还是解决了
下面是我觉得最简单的代码,没有之一
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList ans = new ArrayList();
if(matrix==null) return ans;
int l=0,r = matrix[0].length-1,t = 0,b=matrix.length-1;
while(l<=r&&t<=b){
if(t==b)
{
for(int i=l;i<=r;i++)
{
ans.add(matrix[t][i]);
}
return ans;
}
if(l==r){
for(int i=t;i<=b;i++)
{
ans.add(matrix[i][l]);
}
return ans;
}
//下面是剥圈操作
for(int i=l;i<r;++i) ans.add(matrix[t][i]);
for(int i=t;i<b;++i) ans.add(matrix[i][r]);
for(int i=r;i>l;--i) ans.add(matrix[b][i]);
for(int i=b;i>t;--i) ans.add(matrix[i][l]);
l++;r--;
t++;b--;
}
return ans;
}
}
设置4个变量, l,r,b,t; 其中b和t代表 bottom和 top,l和r 代表left,right
这个题麻烦的就是剥圈和判断是否成圈
剥圈的思路都可以想的出来,但是矩阵是有奇数列和奇数行的,并没有说是一个方阵,而且如何判断这是单独一行或单独一列 或者是一个圈,这是很容易忽略的事
在循环里面添加一个条件判断,如果发现是只剩下一行或只剩下一列了,把这一列丢进ans里面 ,就可以直接返回了