二叉树习题(上)
Posted *平芜尽处是春山*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树习题(上)相关的知识,希望对你有一定的参考价值。
下面是总集呐
100.相同的树
572.另一棵树的子树
110. 平衡二叉树
101.对称二叉树
100.相同的树
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);
运行截图:
572.另一棵树的子树
在这里插入代码片class Solution
private boolean isSameTree(TreeNode p,TreeNode q)
if(p==null&&q==null)
return true;
if(p!=null&&q==null||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 root, TreeNode subRoot)
if(root==null||subRoot==null)
return false;
if(isSameTree(root,subRoot))
return true;
if(isSubtree(root.left,subRoot))
return true;
if(isSubtree(root.right,subRoot))
return true;
return false;
运行截图:
110. 平衡二叉树
class Solution
public boolean isBalanced(TreeNode root)
if(root == null)
return true;
int leftHeight = height(root.left);
int rightHeight = height(root.right);
int heightAbs = Math.abs(leftHeight - rightHeight);
if(heightAbs < -1 || heightAbs > 1)
return false;
return isBalanced(root.left) && isBalanced(root.right);
public int height(TreeNode root)
if(root == null)
return 0;
return 1 + Math.max(height(root.left),height(root.right));
运行截图:
代码优化:
class Solution
public boolean isBalanced(TreeNode root)
if(root == null)
return true;
Map<TreeNode,Integer> map = new HashMap<> ();
int leftHeight = 0;
int rightHeight = 0;
if(map.containsKey(root.left))
leftHeight = map.get(root.left);
else
leftHeight = height(root.left);
map.put(root.left,leftHeight);
if(map.containsKey(root.right))
rightHeight = map.get(root.right);
else
rightHeight = height(root.right);
map.put(root.right,rightHeight);
int heightAbs = Math.abs(leftHeight - rightHeight);
if(heightAbs < -1 || heightAbs > 1)
return false;
return isBalanced(root.left) && isBalanced(root.right);
public int height(TreeNode root)
if(root == null)
return 0;
return 1 + Math.max(height(root.left),height(root.right));
运行截图:
101.对称二叉树
1、普通方法
class Solution
public boolean isSymmetric(TreeNode root)
if(root == null)
return true;
return isMirror(root.left,root.right);
private boolean isMirror(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;
return isMirror(left.left,right.right) && isMirror(left.right,right.left);
运行截图:
2、使用队列
class Solution
public boolean isSymmetric(TreeNode root)
if(root == null || (root.left == null && root.right == null))
return true;
Queue<TreeNode> queue = new LinkedList<> ();
queue.offer(root.left);
queue.offer(root.right);
while(!queue.isEmpty())
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;
运行截图:
以上是关于二叉树习题(上)的主要内容,如果未能解决你的问题,请参考以下文章
二叉树有关习题整理145二叉树的后序遍历 94二叉树的中序遍历 572另一棵树的子树 236二叉树的最近公共祖先 JZ36二叉搜索树与双向链表 - 牛客