# 剑指offer 第十二天

### 58.对称的二叉树

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}

}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
return isSymmetrical(pRoot,pRoot);
}
public boolean isSymmetrical(TreeNode n1,TreeNode n2){
if(n1 == null && n2 == null) return true;
if(n1 == null || n2 == null) return false;
if(n1.val != n2.val) return false;
return isSymmetrical(n1.left,n2.right) && isSymmetrical(n1.right,n2.left);
}
}


### 59.之字形打印二叉树

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if(pRoot == null) return result;
int layer = 1;
Stack<TreeNode> odd = new Stack<>();
Stack<TreeNode> even = new Stack<>();
odd.push(pRoot);
while(!odd.empty()||!even.empty()){
ArrayList<Integer> list = new ArrayList<>();
if((layer & 1) == 1){//位运算，判断是否为奇数;等价于layer%2
while(!odd.empty()){
TreeNode node = odd.pop();
if(node.left != null) even.push(node.left);
if(node.right != null) even.push(node.right);
}
}else{
while(!even.empty()){
TreeNode node = even.pop();
if(node.right != null) odd.push(node.right);
if(node.left != null) odd.push(node.left);
}
}
layer++;
}
return result;
}
}


### 60.把二叉树打印成多行

import java.util.ArrayList;
public class Solution {
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if(pRoot == null) return result;
ArrayList<Integer> list = new ArrayList<>();
queue.offer(pRoot);//将根节点加入队列
int elementsCount = 1;//用于记录每一层的元素个数
TreeNode node = queue.poll();
elementsCount--;
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
if(elementsCount == 0){
list = new ArrayList<>();
elementsCount = queue.size();
}
}
return result;
}
}


### 61.序列化二叉树

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}

}
*/
public class Solution {
String Serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
if(root == null)
sb.append("$,"); else{ sb.append(root.val+","); sb.append(Serialize(root.left)); sb.append(Serialize(root.right)); } return sb.toString(); } int index = -1; TreeNode Deserialize(String str) { if(str == null || str == "") return null; String[] strArray = str.split(","); if(strArray.length == 0) return null; return DeserializeCore(strArray); } TreeNode DeserializeCore(String[] strArray){ TreeNode node = null; index++; if(!strArray[index].equals("$")){
node = new TreeNode(Integer.parseInt(strArray[index]));
node.left = DeserializeCore(strArray);
node.right = DeserializeCore(strArray);
}
return node;
}
}


### 62.二叉搜索树的第K个结点

import java.util.ArrayList;
public class Solution {
ArrayList<TreeNode> list = new ArrayList<>();
TreeNode KthNode(TreeNode pRoot, int k)
{
if(k < 1 || pRoot == null) return null;
LDR(pRoot);
if(list.size() < k) return null;
return list.get(k-1);
}

void LDR(TreeNode pRoot){
if(pRoot.left != null)
LDR(pRoot.left);
if(pRoot.right!=null)
LDR(pRoot.right);
}
}


### 63.数据流中的中位数

import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer i1,Integer i2){
return i2-i1;
}
});
public void Insert(Integer num) {
minHeap.offer(num);
if(minHeap.size()>maxHeap.size()){
maxHeap.offer(minHeap.poll());
}else{
maxHeap.offer(minHeap.poll());
minHeap.offer(maxHeap.poll());
}
}
public Double GetMedian() {
if(minHeap.size()==0&&maxHeap.size()==0)
return null;
if(minHeap.size() == maxHeap.size())
return (double)(minHeap.peek()+maxHeap.peek())/2.0;
return (double)maxHeap.peek();
}
}


### 64.互动窗口的最大值

import java.util.LinkedList;
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
ArrayList<Integer> result = new ArrayList<>();
if(num.length<1||size<1||size>num.length)
return result;
for(int i = 0;i<num.length;i++){
while(!deque.isEmpty() && num[deque.peekLast()] <= num[i])
deque.pollLast();
deque.offerLast(i);
if(i>=size-1){
while(i-deque.peekFirst()>size-1)
deque.pollFirst();
}
}
return result;
}
}


### 65.矩阵中的路径

public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
if(matrix == null || rows<1 || cols<1 || str == null) return false;
boolean[][] visited = new boolean[rows][cols];
int pathLength = 0;
for(int row = 0;row<rows;row++)
for(int col = 0 ;col<cols;col++){
if(hasPathCore(matrix,rows,cols,row,col,str,visited,pathLength))
return true;
}
return false;
}
public boolean hasPathCore(char[] matrix,int rows,int cols,int row,int col,char[] str,boolean[][] visited,int pathLength){
if(pathLength == str.length) return true;
boolean hasPath = false;
if(row>=0 && col>=0 && row<rows && col<cols && visited[row][col]==false && matrix[row*cols+col] == str[pathLength]){
pathLength++;
visited[row][col] = true;
hasPath = hasPathCore(matrix,rows,cols,row+1,col,str,visited,pathLength)
||hasPathCore(matrix,rows,cols,row-1,col,str,visited,pathLength)
||hasPathCore(matrix,rows,cols,row,col+1,str,visited,pathLength)
||hasPathCore(matrix,rows,cols,row,col-1,str,visited,pathLength);
if(!hasPath){
pathLength--;
visited[row][col] = false;
}
}
return hasPath;
}
}


### 66.机器人的运动范围

public class Solution {
public int movingCount(int threshold, int rows, int cols) {
if (threshold < 1 || rows < 1 || cols < 1) return 0;
boolean[][] visited = new boolean[rows][cols];
return movingCountCore(threshold, 0, 0, rows, cols, visited);

}

public int movingCountCore(int threshold, int row, int col, int rows, int cols, boolean[][] visited) {
int count = 0;
if (check(threshold, row, col, rows, cols, visited)) {
visited[row][col] = true;
count = 1 + movingCountCore(threshold, row + 1, col, rows, cols, visited)
+ movingCountCore(threshold, row - 1, col, rows, cols, visited)
+ movingCountCore(threshold, row, col + 1, rows, cols, visited)
+ movingCountCore(threshold, row, col - 1, rows, cols, visited);
}
return count;

}

public boolean check(int threshold, int row, int col, int rows, int cols, boolean[][] visited) {
if (row < 0 || col < 0 || row >= rows || col >= cols || visited[row][col])
return false;
int num = 0;
while (col != 0 || row != 0) {
num += col % 10;
num += row % 10;
col /= 10;
row /= 10;
}
if (num > threshold)
return false;
return true;
}

}


06-02 2019

07-15 4万+

08-18 3334

02-03 3万+

08-29 1132

06-01 1217

07-29 612

02-02 3162

09-08 235

08-11 1472

09-17 7万+

05-02 3511

06-12 4201

01-11 2万+

10-24 8003

08-15 654

04-14 61万+

#### 在中国程序员是青春饭吗？

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客