226. 翻转二叉树
题目
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
答案
//就是左右节点交换
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
//左 右 根
root.left = invertTree(root.left);
root.right = invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode curr = queue.poll();
TreeNode temp = curr.left;
curr.left = curr.right;
curr.right = temp;
if(curr.left!=null) queue.offer(curr.left);
if(curr.right!=null) queue.offer(curr.right);
}
}
return root;
}
}
101. 对称二叉树
题目
给你一个二叉树的根节点 root
, 检查它是否轴对称。
答案
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
boolean flag = deal(root.left,root.right);
return flag;
}
boolean deal(TreeNode left,TreeNode right){
if(left==null && right==null){
return true;
}
//根 左 右
if(left==null || right==null){
return false;
}
if(left.val!=right.val){
return false;
}
boolean flag1 = deal(left.left,right.right);
boolean flag2 = deal(left.right,right.left);
return flag1 && flag2;
}
}
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root.left);
queue.offer(root.right);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode left = queue.poll();
TreeNode right = queue.poll();
//根节点
if(left==null && right==null){
continue;
}
if(left==null || right==null){
return false;
}
if(left.val != right.val){
return false;
}
queue.offer(left.left);
queue.offer(right.right);
queue.offer(left.right);
queue.offer(right.left);
}
}
return true;
}
}
100. 相同的树
题目
给你两棵二叉树的根节点 p
和 q
,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
答案
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;
}
boolean flag1 = isSameTree(p.left,q.left);
boolean flag2 = isSameTree(p.right,q.right);
return flag1 && flag2;
}
}
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null){
return true;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(p);
queue.offer(q);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode left = queue.poll();
TreeNode right = queue.poll();
if(left==null && right==null){
continue;
}
if(left==null || right==null){
return false;
}
if(left.val != right.val){
return false;
}
queue.offer(left.left);
queue.offer(right.left);
queue.offer(left.right);
queue.offer(right.right);
}
}
return true;
}
}
572. 另一棵树的子树
题目
给你两棵二叉树 root
和 subRoot
。检验 root
中是否包含和 subRoot
具有相同结构和节点值的子树。如果存在,返回 true
;否则,返回 false
。
二叉树 tree
的一棵子树包括 tree
的某个节点和这个节点的所有后代节点。tree
也可以看做它自身的一棵子树
答案
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root==null){
return false;
}
//根 左 右
boolean flag = false;
if(root.val==subRoot.val){
flag = deal(root,subRoot);
}
boolean flag1 = isSubtree(root.left,subRoot);
boolean flag2 = isSubtree(root.right,subRoot);
return flag || flag1 || flag2;
}
boolean deal(TreeNode left,TreeNode right){
if(left==null && right==null){
return true;
}
if(left==null || right==null){
return false;
}
if(left.val != right.val){
return false;
}
boolean flag1 = deal(left.left,right.left);
boolean flag2 = deal(left.right,right.right);
return flag1 && flag2;
}
}
104. 二叉树的最大深度
题目
给定一个二叉树 root
,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
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;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
int res = 0;
while(!queue.isEmpty()){
int size = queue.size();
res++;
for(int i=0;i<size;i++){
TreeNode curr = queue.poll();
if(curr.left!=null) queue.offer(curr.left);
if(curr.right!=null) queue.offer(curr.right);
}
}
return res;
}
}
111. 二叉树的最小深度
题目
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
**说明:**叶子节点是指没有子节点的节点。
答案
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
//左 右 根
int left = minDepth(root.left);
int right = minDepth(root.right);
if(root.left==null) return right + 1;
if(root.right==null) return left + 1;
return Math.min(left,right) + 1;
}
}
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
int res = 0;
while(!queue.isEmpty()){
int size = queue.size();
res++;
for(int i=0;i<size;i++){
TreeNode curr = queue.poll();
if(curr.left==null && curr.right==null){
return res;
}
if(curr.left!=null) queue.offer(curr.left);
if(curr.right!=null) queue.offer(curr.right);
}
}
return res;
}
}
222. 完全二叉树的节点个数
题目
给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2h
个节点。
答案
class Solution {
int res = 0;
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
//根 左 右
res++;
countNodes(root.left);
countNodes(root.right);
return res;
}
}
class Solution {
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
int res = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode curr = queue.poll();
res++;
if(curr.left!=null) queue.offer(curr.left);
if(curr.right!=null) queue.offer(curr.right);
}
}
return res;
}
}
110. 平衡二叉树
题目
给定一个二叉树,判断它是否是平衡二叉树
答案
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
//根 左 右 遍历
boolean falg = Math.abs(deal(root.left)-deal(root.right)) < 2;
boolean falg1 = isBalanced(root.left);
boolean falg2 = isBalanced(root.right);
return falg && falg1 && falg2;
}
int deal(TreeNode root){//求 高 度
if(root==null){
return 0;
}
// 左 右 根
int left = deal(root.left);
int right = deal(root.right);
return Math.max(left,right) + 1;
}
}
257. 二叉树的所有路径
题目
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
答案
class Solution {
List<String> res = new ArrayList();
public List<String> binaryTreePaths(TreeNode root) {
if(root==null){
return res;
}
deal(root,"");
return res;
}
void deal(TreeNode root,String str){
if(root==null){
return;
}
if(root.left==null && root.right==null){
res.add(new StringBuilder(str).append(root.val+"").toString());
return;
}
//根 左 右
str = new StringBuilder(str).append(root.val+"").append("->").toString();
deal(root.left,str);
deal(root.right,str);
}
}