zoukankan      html  css  js  c++  java
  • 剑指offer-第六章面试中的各项能力(二叉树的深度)

    题目:1:输入一个二叉树,求二叉树的深度。从根节点开始最长的路径。

    思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1.

    题目2:输入一颗二叉树的根节点,判断该二叉树是不是平衡二叉树。平衡二叉树是这样的数,每一个节点左右子树的深度差不超过1.

    思路1:从根节点开始判断,左右子树的节点的深度是否是相差1的。然后递归判断下面的节点。

    思路2:采用后序遍历,判断左右子树是否相差不超过1,记住每次左右子树的深度。最后判断根节点是否是平衡的。这样做的好处是避免了重复遍历好多节点。从下到上。每个节点只遍历一次。

    Java代码:

    //二叉树的深度,可以求得左右子树中较大的那个值加上1即可
    public class BiTreeDeepth {
        public class BiTreeNode{
            int m_nValue;
            BiTreeNode m_pLeft;
            BiTreeNode m_pRight;
        }
        public BiTreeNode createBiTree(int[] pre,int start,int[] ord,int end,int length){
            if(pre.length!=ord.length||pre==null||ord==null||length<=0)
                return null;
            int value=pre[start];
            BiTreeNode root=new BiTreeNode();
            root.m_nValue=value;
            root.m_pRight=root.m_pLeft=null;
            if(length==1){
                if(pre[start]==ord[end])
                    return root;
                else
                    throw new RuntimeException("inVaild put");
            }
            //遍历中序遍历的序列找到根节点
            int i=0;
            while(i<length){
                if(ord[end-i]==value)
                    break;
                i++;
            }
            int right=i;
            int left=length-i-1;
            if(left>0)
                root.m_pLeft=createBiTree(pre,start+1,ord,end-i-1,length-i-1);
            if(right>0)
                root.m_pRight=createBiTree(pre,start+length-i,ord,end,i);
            return root;
            }
            //求二叉树的深度
            public int treeDepth(BiTreeNode pHead){
                if(pHead==null)
                    return 0;
                int left=treeDepth(pHead.m_pLeft);
                int right=treeDepth(pHead.m_pRight);
                return (left>right)?left+1:right+1;
                
            }
            //判断二叉树是不是平衡二叉树,平衡二叉树是指在二叉树中任意节点的左右子树的深度不超过1
            boolean isBlance(BiTreeNode pHead){
                if(pHead==null)
                    return true;
                int left=treeDepth(pHead.m_pLeft);
                int right=treeDepth(pHead.m_pRight);
                int dif=left-right;
                if(dif>1||dif<-1)
                    return false;
                return isBlance(pHead.m_pLeft)&&isBlance(pHead.m_pRight);
            }
            //判断二叉树是不是平衡二叉树,采用后序遍历的思路,不用遍历重复的节点
            boolean isBlance1(BiTreeNode pHead){
                if(pHead==null)
                    return true;
                int[] a=new int[1];
                a[0]=0;
                return isBlanced(pHead,a);
                
            }
            public boolean isBlanced(BiTreeNode pHead, int[] a) {
                if(pHead==null){
                    a[0]=0;
                    return true;
                }
                int[] left=new int[1];
                int[] right=new int[1];
                if(isBlanced(pHead.m_pLeft,left)&&isBlanced(pHead.m_pRight,right)){
                    int dif=left[0]-right[0];
                    if(dif>=-1&&dif<=1){
                        a[0]=1+((left[0]>right[0])?left[0]:right[0]);
                        return true;
                    }
                        
                        
                }
                return false;
            }
            
          public static void main(String[] args) {
              int pre[] = {1, 2, 4, 7, 3, 5, 6, 8};  
    
              int ord[] = {4, 7, 2, 1, 5, 3, 8, 6};  
              BiTreeDeepth btd=new BiTreeDeepth();
              BiTreeNode pHead=btd.createBiTree(pre, 0, ord, pre.length-1, pre.length);
              int depth=btd.treeDepth(pHead);
              System.out.println(depth);
              boolean isBlanced=btd.isBlance(pHead);
              boolean isBlanced1=btd.isBlance1(pHead);
              System.out.println(isBlanced+""+isBlanced1);
                
        }
    }
  • 相关阅读:
    洛谷P1258小车问题
    洛谷P1028 数的计算
    P1980 计数问题
    洛谷P1907口算练习题
    2018icpc沈阳-K.Let the Flames Begin (约瑟夫环问题)
    Codeforces Round #585 (Div. 2) B. The Number of Products
    字符串部分模板总结
    CF-1209D Cow and Snacks (并查集,图)
    Codeforces Round #584 (div.1+div.2)(补题)
    HDU
  • 原文地址:https://www.cnblogs.com/hupp/p/4769571.html
Copyright © 2011-2022 走看看