二叉树相关习题
文章目录
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);
}
}
//如果两个树都为空返回true,有一个树为空,则不相同,
//都不为空,则判断两个树的根节点是否相同,递归判断两个树的左节点和右节点
2. 判断一个树是否是另一个树的子树
class Solution {
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;
}
public boolean isSameTree(TreeNode root,TreeNode subRoot){
if(root == null && subRoot == null){
return true;
}
if(root == null || subRoot == null){
return false;
}
if(root.val != subRoot.val){
return false;
}
return isSameTree(root.left,subRoot.left)&&isSameTree(root.right,subRoot.right);
}
}
//先判断两个树是否相同,再递归判断一个树是否是另一个树的左子树或右子树
3. 二叉树的最大深度
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int leftMax = maxDepth(root.left);
int rightMax = maxDepth(root.right);
return (leftMax > rightMax)?leftMax +1 : rightMax+1;
}
}
//最大深度就是这个树的高度
4. 判断一颗二叉树是否是平衡二叉树
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
//一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1
return Math.abs(leftHeight - rightHeight)<2 && isBalanced(root.left) && isBalanced(root.right);
}
public int maxDepth(TreeNode root){
if(root == null){
return 0;
}
int leftMax = maxDepth(root.left);
int rightMax = maxDepth(root.right);
return (leftMax > rightMax)? leftMax+1 : rightMax +1;
}
}
//先求出一个二叉树左右子树的高度,判断两者之差是否小于2,再递归判断左子树和右子树
5. 对称二叉树
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
return isSymmetricChild(root.left,root.right);
}
public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
if(leftTree ==null && rightTree == null){
return true;
}
if(leftTree == null || rightTree == null){
return false;
}
if(leftTree.val != rightTree.val){
return false;
}
//递归判断左子树的左和右子树的右,左子树的右和右子树的左
return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
}
}
6. 二叉树的最近公共祖先
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return null;
}
//如果 q 或者 p 是root, 则root就是最近的公共祖先
if(q == root || p == root){
return root;
}
//不是root。 则去root的左树和右数去找
TreeNode leftTree = lowestCommonAncestor(root.left,q,p);
TreeNode rightTree = lowestCommonAncestor(root.right,q,p);
//如果左树和右树都找到了,说明root是最近公共祖先
if(leftTree != null && rightTree != null){
return root;
}
//如果左树找到了 右树没找到 说明左树找到的这个节点就是公共祖先
if(leftTree != null){
return leftTree;
}
//如果右树找到了 左树没找到, 说明右树找到的这个节点 就是公共祖先
if(rightTree != null){
return rightTree;
}
return null;
}
}
7. 二叉搜索树与双向链表
public class Solution {
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null){
return null;
}
ConvertChild(pRootOfTree);
TreeNode tmp = pRootOfTree;
while(tmp.left != null){
tmp = tmp.left;
}
return tmp;
}
static TreeNode prev = null;
public void ConvertChild(TreeNode pCur) {
if(pCur == null){
return;
}
ConvertChild(pCur.left);
pCur.left = prev;
if(prev != null){
prev.right = pCur;
}
prev = pCur;
ConvertChild(pCur.right);
}
}
//二叉搜索树,中序遍历是有序的,