101. 对称二叉树
力扣题目链接(opens new window)
给定一个二叉树,检查它是否是镜像对称的。
递归
/*** 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 compare(TreeNode left,TreeNode right){if(left == null && right == null) return true;//左右节点都为空else if(left != null && right == null) return false;//左不空,右空,不可能对称else if(left ==null && right != null) return false;//右不空,左空,不可能对称else if(left.val != right.val) return false;//左右都不为空,但不相等else{//左右都不为空,且相当,进入下一次的递归判断boolean out = compare(left.left,right.right);//左,右边树的外侧节点判断boolean inside = compare(left.right,right.left);//左,右边树的内侧节点判断return (out&& inside);}}public boolean isSymmetric(TreeNode root) {return compare(root.left,root.right);}
}
迭代
/*** 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 isSymmetric(TreeNode root) {Stack<TreeNode> stack = new Stack<>();//栈辅助if(root == null) return true;stack.push(root.left);stack.push(root.right);while(!stack.isEmpty()){TreeNode left = stack.peek();//获得栈顶结点stack.pop();//弹出栈顶结点TreeNode right = stack.peek();//再次获得栈顶结点stack.pop();//再弹出栈顶结点if(left ==null&&right==null) continue;//左右都为空,继续下一次循环else if(left != null && right == null) return false;else if(left ==null && right != null) return false;else if(left.val != right.val) return false;else{//左右都不为空,且相等stack.push(left.left);stack.push(right.right);stack.push(left.right);stack.push(right.left);}}return true;}
}
2,力扣100,相同的树
给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例 1:输入:p = [1,2,3], q = [1,2,3]
输出:true
示例 2:输入:p = [1,2], q = [1,null,2]
输出:false
示例 3:输入:p = [1,2,1], q = [1,1,2]
输出:false提示:两棵树上的节点数目都在范围 [0, 100] 内
-104 <= Node.val <= 104
/*** 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;//左右节点都为空else if(p != null && q == null) return false;//左不空,右空,不可能对称else if(p ==null && q != null) return false;//右不空,左空,不可能对称else if(p.val != q.val) return false;//左右都不为空,但不相等else{//左右都不为空,且相当,进入下一次的递归判断boolean out = isSameTree(p.left,q.left);boolean inside = isSameTree(p.right,q.right);return (out&& inside);}}
}
3 力扣572,另一棵子树
给你两棵二叉树 root
和 subRoot
。检验 root
中是否包含和 subRoot
具有相同结构和节点值的子树。如果存在,返回 true
;否则,返回 false
。
二叉树 tree
的一棵子树包括 tree
的某个节点和这个节点的所有后代节点。tree
也可以看做它自身的一棵子树。
示例 1:
输入:root = [3,4,5,1,2], subRoot = [4,1,2] 输出:true
示例 2:
输入:root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2] 输出:false
/*** 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 root,TreeNode subRoot){if(root == null && subRoot == null) return true;//左右节点都为空else if(root != null && subRoot == null) return false;//左不空,右空,不可能对称else if(root ==null && subRoot != null) return false;//右不空,左空,不可能对称else if(root.val != subRoot.val) return false;//左右都不为空,但不相等else{//左右都不为空,且相当,进入下一次的递归判断boolean out = isSametree(root.left,subRoot.left);boolean inside = isSametree(root.right,subRoot.right);return (out&& inside);}}public boolean isSubtree(TreeNode root, TreeNode subRoot) {if(root==null&&subRoot==null) return true;else if(root==null && subRoot!=null) return false;else if(root!=null && subRoot == null) return true;else if(isSametree(root,subRoot)) return true;else{return isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);}}}