二叉树相关习题
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);
}
}
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);
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;
}
}
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;
}
if(q == root || p == root){
return root;
}
TreeNode leftTree = lowestCommonAncestor(root.left,q,p);
TreeNode rightTree = lowestCommonAncestor(root.right,q,p);
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);
}
}