100.相同的树
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}
if(p==null||q==null){
return false;
}
return p.val==q.val&&isSameTree(p.left, q.left)&&isSameTree(p.right,q.right);
}
}
101.对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isMirrorTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}
if(p==null||q==null){
return false;
}
return p.val==q.val&&isMirrorTree(p.left, q.right)&&isMirrorTree(q.left, p.right);
}
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return isMirrorTree(root.left, root.right);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>();
List<List<Integer>> rets=new ArrayList<>();
if(root!=null){
queue.offer(root);
}
while(!queue.isEmpty()){
int count=queue.size();
List<Integer> list=new ArrayList<>();
while(count>0){
//1.拿到队头元素,把队头元素的左右子树入队
TreeNode cur=queue.poll();
System.out.println(cur.val+" ");
list.add(cur.val);
//2.不为空的时候才能出队
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!=null){
queue.offer(cur.right);
}
count--;
}
rets.add(list);
}
return rets;
}
}
104.二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}
Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0) {
return null;
}
int rootValue = preorder[0];
int leftCount;
for (leftCount = 0; leftCount < inorder.length; leftCount++) {
if (inorder[leftCount] == rootValue) {
break;
}
}
TreeNode root = new TreeNode(rootValue);
int[] leftPreorder = Arrays.copyOfRange(preorder,
1, 1 + leftCount);
int[] leftInorder = Arrays.copyOfRange(inorder, 0, leftCount);
root.left = buildTree(leftPreorder, leftInorder);
int[] rightPreorder = Arrays.copyOfRange(preorder,
1 + leftCount, preorder.length);
int[] rightInorder = Arrays.copyOfRange(inorder,
leftCount + 1, inorder.length);
root.right = buildTree(rightPreorder, rightInorder);
return root;
}
}
106. 从中序与后序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0){
return null;
}
int rootValues = postorder[postorder.length-1];
TreeNode root = new TreeNode(rootValues);
int leftCount = 0;
for(leftCount = 0;leftCount < inorder.length;leftCount++){
if(inorder[leftCount] == rootValues){
break;
}
}
//统计出了左子树的节点数后求出左子树中序遍历的数组
int[] leftInorder = Arrays.copyOfRange(inorder,0,leftCount);
int[] leftPostorder = Arrays.copyOfRange(postorder,0,leftCount);
root.left = buildTree(leftInorder,leftPostorder);
//建右子树
int[] rightInorder = Arrays.copyOfRange(inorder,leftCount+1,inorder.length);
int[] rightPostorder = Arrays.copyOfRange(postorder,leftCount,inorder.length-1);
root.right = buildTree(rightInorder,rightPostorder);
return root;
}
}
110.平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
class Solution {
public int getHeight(TreeNode root){
if(root==null){
return 0;
}
int left=getHeight(root.left);
int right=getHeight(root.right);
return Math.max(left, right)+1;
}
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
if(!isBalanced(root.left)){
return false;
}
if(!isBalanced(root.right)){
return false;
}
int left=getHeight(root.left);
int right=getHeight(root.right);
int diff=left-right;
if(diff>=-1&&diff<=1){
return true;
}
return false;
}
}
236.二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean search(TreeNode r,TreeNode t){
if(r==null){
return false;
}
if(r==t){
return true;
}
if(search(r.left,t)){
return true;
}else{
return false;
}
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p==root||q==root){
return root;
}
boolean pInLeft=search(root.left,p);
boolean qInRight=search(root.Right,q);
if(pInLeft&&qInLeft){
return lowestCommonAncestor(root.left,p,q);
}
if(pInRight&&qInRight){
return lowestCommonAncestor(root.right,p,q);
}
return root;
}
}
572.另一个树的子树
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}
if(p==null||q==null){
return false;
}
return p.val==q.val&&isSameTree(p.left, q.left)&&isSameTree(p.right,q.right);
}
public boolean search(TreeNode root,TreeNode t){
if(root==null){
return false;
}
if(isSameTree(root,t)){
return true;
}
if(search(root.left,t)){
return true;
}
return search(root.right,t);
}
public boolean isSubtree(TreeNode s, TreeNode t) {
return search(s,t);
}
}