文章目录
- 1. JZ36 二叉搜索树与双向链表
- 2. 100. 相同的树
- 3. 572. 另一棵树的子树
- 4. BM26 求二叉树的层序遍历
- 5. BM33 二叉树的镜像
- 6. BM40 重建二叉树
- 7. 106. 从中序与后序遍历序列构造二叉树
1. JZ36 二叉搜索树与双向链表
JZ36 二叉搜索树与双向链表
解题思路:
由题目可知,这是一颗二叉搜索树.二叉搜索树的特点就是他的中序遍历是有序的.所以本题我们大的框架就是要在中序遍历里完成.具体解题如下:
代码:
/**
public class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;}}
*/
public class Solution {public TreeNode Convert(TreeNode pRootOfTree) {if(pRootOfTree == null){return null;}TreeNode head = pRootOfTree;convertChild(head);while(head.left != null){head = head.left;}return head;}public static TreeNode prev = null;public static void convertChild(TreeNode pCur){if(pCur == null){return;}convertChild(pCur.left);pCur.left = prev;if(prev != null){prev.right = pCur;}prev = pCur;convertChild(pCur.right);}
}
2. 100. 相同的树
100. 相同的树
解题思路:
要想判断两颗二叉树是否相同,那么就要从两个方面去考虑:
- 二叉树的结构
- 二叉树的值
本题我们采用递归的思想,则代码如下:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {//如果两棵树都为空,则相同if(p == null && q == null){return true;}//如果两颗树其中一颗为空,则不同if(p != null && q == null || p == null && q!= null){return false;}if(p.val != q.val){return false;}return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);}
}
3. 572. 另一棵树的子树
572. 另一棵树的子树
解题思路:
想要判断一棵树是不是另一颗树的子树,我们可以采用递归的思想.先判断子树是不是这棵树的左树,在判断是不是这棵树的右树.要是都不是,则这棵子树不是树的子树.则有以下代码:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public boolean isSubtree(TreeNode root, TreeNode subRoot) {if(root== null){return false;}if(isSameTree(root,subRoot)){return true;}if(isSameTree(root.left,subRoot)){return true;}if(isSameTree(root.right,subRoot)){return true;}return false;}public boolean isSameTree(TreeNode p, TreeNode q) {//如果两棵树都为空,则相同if(p == null && q == null){return true;}//如果两颗树其中一颗为空,则不同if(p != null && q == null || p == null && q!= null){return false;}if(p.val != q.val){return false;}return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);}
}
4. BM26 求二叉树的层序遍历
BM26 求二叉树的层序遍历
解题思路:
import java.util.*;/** public class TreeNode {* int val = 0;* TreeNode left = null;* TreeNode right = null;* public TreeNode(int val) {* this.val = val;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>>*/public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {// write code here//先创建一个ListList列表用来存放ArrayList<ArrayList<Integer>> list = new ArrayList<>();if(root == null){return list;}Queue<TreeNode> qu = new LinkedList<>();qu.offer(root);while(!qu.isEmpty()){int size = qu.size();ArrayList<Integer> tmp = new ArrayList<>();while(size > 0){TreeNode cur = qu.poll();size--;tmp.add(cur.val);if(cur.left != null){qu.offer(cur.left);}if(cur.right != null){qu.offer(cur.right);}}list.add(tmp);}return list;}
}
5. BM33 二叉树的镜像
BM33 二叉树的镜像
解题思路:
本题我们采用子问题的思路.根据二叉镜像树的特点.我们可以看出来我们需要交换每一个根的左右节点.在代码中我们可以先递归左子树,然后让它的子节点交换,再递归右子树(此时代码中书写的仍然是左,因为在上述交换过程中已经交换了).代码如下:
import java.util.*;/** public class TreeNode {* int val = 0;* TreeNode left = null;* TreeNode right = null;* public TreeNode(int val) {* this.val = val;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param pRoot TreeNode类 * @return TreeNode类*/public TreeNode Mirror (TreeNode root) {// write code hereif(root == null){ return null;}Mirror(root.left);TreeNode tmp = root.left;root.left = root.right;root.right = tmp;Mirror(root.left);return root;}
}
6. BM40 重建二叉树
BM40 重建二叉树
解题思路:根据前序遍历和中序遍历的特点,我们可以知道前序遍历的第一个字符"1"为二叉树的根.我们需要在中序遍历中找到"1"的位置,然后1的左边为左子树,"1"的右边为右子树.按照以上的思想,我们可以通过前序遍历和中序遍历构建出这棵树,如下所示:
代码具体实现如下:
import java.util.*;/** public class TreeNode {* int val = 0;* TreeNode left = null;* TreeNode right = null;* public TreeNode(int val) {* this.val = val;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param preOrder int整型一维数组 * @param vinOrder int整型一维数组 * @return TreeNode类*/public int preIndex = 0;public TreeNode reConstructBinaryTree (int[] preorder, int[] inorder) {// write code herereturn buildTree(preorder,inorder,0,inorder.length-1);}// preorder:前序遍历数组下标// inbegin:中序遍历的首部// inend:中序遍历的结束private TreeNode buildTree(int[] preorder,int[] inorder,int inbegin,int inend){// 1.判断当前根结点是不是还有左子树或者右子树if(inbegin > inend){return null;}TreeNode root = new TreeNode(preorder[preIndex]);// 2.找到root在中序遍历的位置int rootIndex = findIndex(inorder,inbegin,inend,preorder[preIndex]);preIndex++;if(rootIndex == -1){return null;}// 构建左子树和右子树root.left = buildTree(preorder,inorder,inbegin,rootIndex-1);root.right = buildTree(preorder,inorder,rootIndex+1,inend);return root;}private int findIndex (int[] inorder,int inbegin,int inend,int val) {for(int i = inbegin;i <= inend;i++) {if(inorder[i] == val) {return i;}}return -1;}
}
7. 106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树
解题思路:本题的思路与上述第六题使用前序遍历和中序遍历重建二叉树的思路是类似的.但是需要注意的是后序遍历的顺序是左右根.所以创建树的顺序就是 根->右子树->左子树
.我们只需要改一下代码的顺序.具体代码的实现如下:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int postIndex = 0;public TreeNode buildTree(int[] inorder, int[] postorder) {// write code herepostIndex = postorder.length-1;return buildTree(postorder,inorder,0,inorder.length-1);}// postorder:后序遍历数组下标// inbegin:中序遍历的首部// inend:中序遍历的结束private TreeNode buildTree(int[] postorder,int[] inorder,int inbegin,int inend){// 1.判断当前根结点是不是还有左子树或者右子树if(inbegin > inend){return null;}TreeNode root = new TreeNode(postorder[postIndex]);// 2.找到root在后序遍历的位置int rootIndex = findIndex(inorder,inbegin,inend,postorder[postIndex]);postIndex--;if(rootIndex == -1){return null;}// 构建右子树和左子树root.right = buildTree(postorder,inorder,rootIndex+1,inend);root.left = buildTree(postorder,inorder,inbegin,rootIndex-1);return root;}private int findIndex (int[] inorder,int inbegin,int inend,int val) {for(int i = inbegin;i <= inend;i++) {if(inorder[i] == val) {return i;}}return -1;}
}