目录
1.创建二叉树
LeetCode105: 通过前中序创建二叉树
前中序创建二叉树分析:
/* * 前序: 根左右 1,2,4,3,6,7 * 中序: 左根右 4,2,1,6,3,7 * * 构建二叉树 * * 前序:根 1 * 中序:根节点前面的是左子树,后面的是右子树 * 左: 4,2 * 右: 6,3,7 * * 前序: 2,4 中序: 4,2 * 前序:根 2 * 左子树 4 * * 前序: 3,6,7 中序: 6,3,7 * 前序 根 3 * 左子树 6 * 右子树: 7 * */
代码:
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0|| inorder.length == 0){
return null;
}
int rootValue = preorder[0];
TreeNode root = new TreeNode(rootValue);
for (int i = 0; i < inorder.length; i++) {
if (inorder[i] == rootValue) {
int[] inLeft = Arrays.copyOfRange(inorder, 0, i);//含头不含尾 [4,2]
int[] inRigth = Arrays.copyOfRange(inorder, i + 1, inorder.length);//[6,3,7]
int[] preLeft = Arrays.copyOfRange(preorder, 1, i + 1);//[2,4] inLeft的数组长度与preLeft相同
int[] preRigth = Arrays.copyOfRange(preorder, i + 1, preorder.length);//[3,6,7]
root.left = buildTree(preLeft, inLeft);
root.right = buildTree(preRigth, inRigth);
break;
}
}
return root;
}
LeetCode106:通过中后序创建二叉树
代码:
public TreeNode buildTree(int[] inorder, int[] postorder){
if(inorder.length==0||postorder.length==0){
return null;
}
//根节点
int rootValue=postorder[postorder.length-1];
TreeNode root=new TreeNode(rootValue);
//划分子树
for(int i=0;i<inorder.length;i++){
if(inorder[i]==rootValue){
//左右子树
int []inLeft= Arrays.copyOfRange(inorder,0,i);//含头不含尾 [4,2]
int []inRigth= Arrays.copyOfRange(inorder,i+1,inorder.length);//[6,3,7]
int[]postLeft= Arrays.copyOfRange(postorder,0,i);//[4,2]
int[]postRight = Arrays.copyOfRange(postorder,i,postorder.length-1);
root.left=buildTree(inLeft,postLeft);
root.right=buildTree(inRigth,postRight);
break;
}
}
return root;
}
2.LeetCode101:对称二叉树
代码:
public static boolean isSymmetric(TreeNode root) {
return check(root.left, root.right);
}
private static boolean check(TreeNode left, TreeNode right) {
if(left==null&&right==null){
return true;
}
//下面的是在上面的前提下进行的,left==null->说明right!=null,所以返回false
if(left==null||right==null){
return false;
}
if(left.val!=right.val){
return false;
}
return check(left.left, right.right)&&check(left.right, right.left);
}
3.二叉树的深度
LeetCode104:二叉树的最大深度
代码:
//解法1 递归后序遍历
public static int maxDepth1(TreeNode root)
{
if(root==null){
return 0;
}
int d1 = maxDepth1(root.left);
int d2 = maxDepth1(root.right);
return Integer.max(d1,d2)+1;
}
//解法2 非递归遍历
public static int maxDepth2(TreeNode root){
LinkedList<TreeNode> stack=new LinkedList<>();
TreeNode curr=root;
TreeNode pop=null;
int max=0;
while(curr!=null||!stack.isEmpty()){
if(curr!=null) {
stack.push(curr);
int size=stack.size();
if(size>max){
max=size;
}
curr = curr.left;
} else{
TreeNode peek=stack.peek();
if(peek.right==null||pop==peek.right){
pop=stack.pop();
}else{
curr=peek.right;
}
}
}
return max;
}
//解法3 层序遍历
public static int maxDepth(TreeNode root){
if(root==null){
return 0;
}
Queue<TreeNode> queue=new LinkedList<>();//jdk自带的
queue.offer(root);
// int c1=1;//记录每层的节点数 1代表根节点
int depth=0;
while(!queue.isEmpty()){
// int c2=0;//每层的节点数 记录下一层的节点数
int size=queue.size();
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
// System.out.print(poll.val+" ");
if(poll.left!=null){
queue.offer(poll.left);
// c2++;
}
if(poll.right!=null){
queue.offer(poll.right);
// c2++;
}
}
// c1=c2;
// System.out.println();
depth++;
}
return depth;
}
二叉树的最小深度
代码:
// 解法1 递归
public static int minDepth1(TreeNode root){
if(root==null){
return 0;
}
int d1 = minDepth1(root.left);
int d2 = minDepth1(root.right);
if(d1==0||d2==0){ //左子树为空或者右子树为空
return d1+d2+1;
}
return Integer.min(d1,d2)+1;
}
// 解法2 层序遍历 遇到第一个叶子节点就是最小深度
public static int minDepth(TreeNode root){
if(root==null){
return 0;
}
Queue<TreeNode> queue=new LinkedList<>();//jdk自带的
queue.offer(root);
int depth=0;
while(!queue.isEmpty()){
int size=queue.size();
depth++;
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
if(poll.left==null&&poll.right==null){//找到叶子节点
return depth;
}
if(poll.left!=null){
queue.offer(poll.left);
}
if(poll.right!=null){
queue.offer(poll.right);
}
}
}
return depth;
}
4.LeetCode:226翻转二叉树
代码:
public static TreeNode reverseTree(TreeNode root)
{
reverse(root);
return root;
}
//节点交换是附带这下面的也交换
private static void reverse(TreeNode node) {
if(node==null){
return;
}
TreeNode temp=node.left;
node.left=node.right;
node.right=temp;
reverse(node.left);//左孩子节点进行交换
reverse(node.right);//右孩子节点进行交换
}
5.根据后缀表达式建立二叉树
后缀表达式建立二叉树分析
/* * 中缀表达式: (2-1)*3 * 后缀表达式: 21-3* * 表达式树: 叶子节点都是操作数, * * * / \ * - 3 * / \ * 2 1 * * * */
代码:
private static TreeNode createExpressionTree(String[] str){
LinkedList<TreeNode> stack = new LinkedList<>();
for(String s:str){
if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
TreeNode right = stack.pop();
TreeNode left = stack.pop();
TreeNode parent = new TreeNode(s);
parent.left = left;
parent.right = right;
stack.push(parent);//父节点压入栈,接着和下一个节点比较
}else{
stack.push(new TreeNode(s));
}
}
return stack.pop();
}