1.计算二叉树的深度:
二叉树的深度等于左子树和右子树的深度大的那一个值加一,当root为null时,返回0。
public int getHeight(Shu root)
{
if(root==null)
{
return 0;
}
int a=getHeight(root.left);
int b=getHeight(root.right);
return a>b?a+1:b+1;
}
2.另一棵树的子树问题
subRoot要么是root本身,要么是root的左子树,要么是root的右子树
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root==null||subRoot==null)
{
return false;
}
if(issame(root,subRoot))
{
return true;
}
if(isSubtree(root.left,subRoot))
{
return true;
}
if(isSubtree(root.right,subRoot))
{
return true;
}
return false;
}
public boolean issame(TreeNode root,TreeNode subRoot)
{
if(root==null&&subRoot!=null||root!=null&&subRoot==null)
{
return false;
}
if(root==null&&subRoot==null)
{
return true;
}
if(root.val!=subRoot.val)
{
return false;
}
if(!issame(root.left,subRoot.left)||!issame(root.right,subRoot.right))
{
return false;
}
return true;
}
3.判断两棵树是否相同
先判断根结点是否相同,然后判断左子树是否相同,然后再判断右子树是否相同
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q!=null||p!=null&&q==null)
{
return false;
}
if(p==null&&q==null)
{
return true;
}
if(p.val!=q.val)
{
return false;
}
if(!isSameTree(p.left,q.left)||!isSameTree(p.right,q.right))
{
return false;
}
//if(!isSameTree(p.right,q.right))
//{
//return false;
// }
return true;
}
4.判断是不是平衡二叉树
一棵高度平衡的二叉树可以定义为:一个二叉树左右的两个子树的高度的差的绝对值不大于1,当二叉树为空树时一定是平衡二叉树
public boolean isBalanced(TreeNode root) {
if(root==null)
{
return true;
}
int d=getHeight(root.left);
int f=getHeight(root.right);
if(Math.abs(d-f)<=1&&isBalanced(root.left)&&isBalanced(root.right))
{
return true;
}
return false;
}
public int getHeight(TreeNode a)
{
if(a==null)
{
return 0;
}
int b=getHeight(a.left);
int c=getHeight(a.right);
return b>c?b+1:c+1;
}
}
5.对称二叉树的判断
public boolean isSymmetric(TreeNode root) {
if(root==null)
{
return true;
}
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode root1,TreeNode root2)
{
if(root1==null&&root2!=null||root2==null&&root1!=null)
{
return false;
}
if(root1==null&&root2==null)
{
return true;
}
if(root1.val!=root2.val)
{
return false;
}
return isSymmetric(root1.left,root2.right)&&isSymmetric(root1.right,root2.left);
}
6.最近公共祖先问题:
这个题的解题思路是什么呢?这道题我们可以这样考虑:我们可以先建立两个栈,然后分别遍历二叉树去找结点,每经过一个结点,就往堆里放,直到找到目标结点,这样就会形成两条路径,如果一个结点左子树右子树都为空,那么这个结点肯定 不是路径上的点,我们把它弹出即可,最后,两个栈里就会放入两条路径上的点,这时我们只需把栈里多的元素先弹出去,直到两个栈元素数相等,然后再一起弹,如果弹出的两个结点相同,那么这个节点就是最近公共节点。
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p==null||q==null)
{
return null;
}
Stack<TreeNode>a=new Stack<>();
Stack<TreeNode>b=new Stack<>();
int sz1=0;
int sz2=0;
if(find(root,p,a))
{
sz1=a.size();
}
if(find(root,q,b))
{
sz2=b.size();
}
int sz=0;
if(sz1>sz2)
{
sz=sz1-sz2;
while(sz!=0)
{
a.pop();
sz--;
}
}
else
{
sz=sz2-sz1;
while(sz!=0)
{
b.pop();
sz--;
}
}
while(!a.empty())
{
TreeNode cur1=a.pop();
TreeNode cur2=b.pop();
if(cur1==cur2)
{
return cur1;
}
}
return null;
}
public boolean find(TreeNode root,TreeNode p,Stack<TreeNode>a)
{
if(root==null)
{
return false;
}
a.push(root);
if(root==p)
{
return true;
}
if(find(root.left,p,a))
{
return true;
}
if(find(root.right,p,a))
{
return true;
}
a.pop();
return false;
}
二叉搜素树转换成排序双向链表
public TreeNode prev;
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree==null)
{
return null;
}
pai(pRootOfTree);
TreeNode cur=pRootOfTree;
while(cur.left!=null)
{
cur=cur.left;
}
return cur;
}
public void pai(TreeNode root)
{
if(root==null)
{
return;
}
pai(root.left);
root.left=prev;
if(prev!=null)
{
prev.right=root;
}
prev=root;
pai(root.right);
}