1.相同的树
Leetcode 100;easy;
package tree;
import java.util.LinkedList;
import java.util.Queue;
public class Main0100相同的树 {
public static void main(String[] args) {
TreeNode root1 = new TreeNode(1);
TreeNode root2 = new TreeNode(1);
System.out.println(new Solution100().isSameTree(root1, root2));
}
}
// 递归
class Solution100 {
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);
}
}
// 迭代
class Solution0100 {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(p);
queue.add(q);
while (!queue.isEmpty()) {
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
if (node1 == null && node2 == null)
continue;
if (node1 == null || node2 == null)
return false;
if (node1.val != node2.val)
return false;
queue.add(node1.left);
queue.add(node2.left);
queue.add(node1.right);
queue.add(node2.right);
}
return true;
}
}
2.二叉树的镜像
Leetcode 101;easy;
package tree;
import java.util.LinkedList;
import java.util.Queue;
public class Main0101对称二叉树 {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(2);
root.left = node1;
root.right = node2;
boolean b = new Solution101().isSymmetric(root);
System.out.println(b);
}
}
class Solution101 {
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
public boolean isSymmetric(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null)
return true;
if (root1 == null || root2 == null)
return false;
if (root1.val != root2.val)
return false;
return isSymmetric(root1.left, root2.right) && isSymmetric(root1.right, root2.left);
}
}
// 迭代
class Solution0101 {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
queue.add(root);
while (!queue.isEmpty()) {
TreeNode root1 = queue.poll();
TreeNode root2 = queue.poll();
if (root1 == null && root2 == null)
continue;
if (root1 == null || root2 == null)
return false;
if (root1.val != root2.val)
return false;
queue.add(root1.left);
queue.add(root2.right);
queue.add(root1.right);
queue.add(root2.left);
}
return true;
}
}
3.另一棵树的子树、拓扑结构
Leetcode 572;easy;
package tree;
public class Main0572另一个树的子树 {
public static void main(String[] args) {
TreeNode root1 = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(3);
root1.left = node1;
root1.right = node2;
TreeNode root2 = new TreeNode(1);
TreeNode node3 = new TreeNode(2);
root2.left = node3;
boolean b = new Solution572().isSubtree(root1, root2);
System.out.println(b);
}
}
// 另一个树的子树
class Solution572 {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null)
return false;
if (t == null)
return true;
return isSame(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}
public boolean isSame(TreeNode s, TreeNode t) {
if (s == null && t == null)
return true;
if (s == null || t == null)
return false;
if(s.val != t.val)
return false;
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}
// 含有另一个树的拓扑结构
class Solution0572 {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (t == null)
return true;
if (s == null)
return false;
return isSame(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSame(TreeNode s, TreeNode t) {
if (t == null)
return true;
if (s == null)
return false;
if (s.val != t.val)
return false;
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}