package day0401;
import java.util.Stack;
/**
* @hurusea
* @create2020-04-01 10:26
*/
public class Solution {
/**
* 输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
* B是A的子结构, 即 A中有出现和B相同的结构和节点值。
* @param A
* @param B
* @return
*/
public boolean isSubStructure(TreeNode A, TreeNode B) {
return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B)
|| isSubStructure(A.right, B));
}
boolean recur(TreeNode A, TreeNode B) {
if(B == null) return true;
if(A == null || A.val != B.val) return false;
return recur(A.left, B.left) && recur(A.right, B.right);
}
/**
* 请完成一个函数,输入一个二叉树,该函数输出它的镜像。
* @param root
* @return
*
*/
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;
}
/**
* 请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
return root == null ? true : search(root.left, root.right);
}
boolean search(TreeNode L, TreeNode R) {
if(L == null && R == null) return true;
if(L == null || R == null || L.val != R.val) return false;
return recur(L.left, R.right) && recur(L.right, R.left);
}
/**
* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
* @param matrix
* @return
*/
public int[] spiralOrder(int[][] matrix) {
if(matrix.length == 0) return new int[0];
int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
int[] res = new int[(r + 1) * (b + 1)];
while(true) {
for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.
if(++t > b) break;
for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.
if(l > --r) break;
for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.
if(t > --b) break;
for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.
if(++l > r) break;
}
return res;
}
/**
* 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。
* 假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,
* 序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
*
* @param pushed
* @param popped
* @return
* 使用一个栈,开始为空 持续压入pushed数组元素到栈中,直到栈顶元素和popped首元素相同,开始弹出,若弹出后还是匹配,继续弹出
* 最后判断栈是否为空,空则true,否则false
*/
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int index = 0;
for (int i = 0, len = pushed.length; i < len; i++) {
stack.push(pushed[i]);
while (!stack.isEmpty() && index < len && popped[index] == stack.peek()) {
stack.pop();
index++;
}
}
return stack.isEmpty();
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
剑指offer0401
最新推荐文章于 2024-03-19 10:30:28 发布