1、判断两个二叉树是否相同
给定两个二叉树,编写一个函数来检验它们是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
具体代码:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null){
return true;
}
if(p == null || q == null){
return false;
}
if(p.val != q.val){
return false;
}
return isSameTree(p.left,q.left)
&&isSameTree(p.right,q.right);
}
}
运行结果:
2、判断是否是另一个数的子树
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
具体代码:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null){
return true;
}
if(p == null || q == null){
return false;
}
if(p.val != q.val){
return false;
}
return isSameTree(p.left,q.left)
&&isSameTree(p.right,q.right);
}
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s == null && t == null){
return true;
}
if(s == null || t == null){
return false;
}
boolean ret = false;
if(s.val == t.val){
ret = isSameTree(s,t);
}
if(!ret){
ret = isSubtree(s.left,t);
}
if(!ret){
ret = isSubtree(s.right,t);
}
return ret;
}
}
运行结果:
3、二叉树的最大深度
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。
具体代码:
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 1;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return 1+(leftDepth>rightDepth?leftDepth:rightDepth);
}
}
运行结果:
4、判断是否是平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
具体代码:
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 1;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return 1+(leftDepth>rightDepth?leftDepth:rightDepth);
}
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
if(root.left == null && root.right == null){
return true;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
if(leftDepth-rightDepth>1 || rightDepth-leftDepth>1){
return false;
}
return isBalanced(root.left)
&& isBalanced(root.right);
}
}
运行结果:
5、判断是否是对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
具体代码:
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
return isMirror(root.left,root.right);
}
public boolean isMirror(TreeNode t1,TreeNode t2){
if(t1 == null && t2 == null){
return true;
}
if(t1 == null || t2 == null){
return false;
}
if(t1.val != t2.val){
return false;
}
return isMirror(t1.left,t2.right)
&& isMirror(t1.right,t2.left);
}
}
运行结果:
6、判断是否是完全二叉树
具体代码:
class Solution {
public boolean isCpmplete(TreeNode root){
if(root == null){
return true;
}
boolean needNoChild = false;
Queue<TreeNode>queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode cur = queue.poll();
if(cur.left != null && cur.right != null){
queue.offer(root.left);
queue.offer(root.right);
} else if(cur.left == null && cur.right != null){
return false;
} else if(cur.left != null && cur.right == null){
queue.offer(root.left);
needNoChild =true;
} else {
needNoChild = true;
}
} else {
if(cur.left != null || cur.right != null){
return fasle;
}
}
return true;
}
}