二叉树

//先序遍历
    public static void preOrder(BinaryTree root){
        if(root==null)
            return;
            System.out.print(root.data+" ");
            preOrder(root.lChild);
            preOrder(root.rChild);
    }
    
    /**
     * 深度优先遍历,相当于先根遍历
     * 采用非递归实现
     * 需要辅助数据结构:栈
     */
    public static void preOrderByStack(BinaryTree root){
        if(root==null){
            System.out.print("empty tree");
            return;
        }
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        stack.push(root);
        while(!stack.isEmpty()){
            BinaryTree tree = stack.pop();
            System.out.print(tree.data);
            if(tree.rChild!=null){
                stack.push(tree.rChild);
            }
            if(tree.lChild!=null){
                stack.push(tree.lChild);
            }
        }
        System.out.println("");
    }
    
     /**
     * 广度优先遍历
     * 采用非递归实现
     * 需要辅助数据结构:队列
     */
    
    public static void levelOrderBystatck(BinaryTree root){
        if(root==null){
            return;
        }
        Queue<BinaryTree> queue = new ArrayDeque<BinaryTree>();
        queue.add(root);
        while(!queue.isEmpty()){
            BinaryTree tree = queue.remove();
            System.out.print(tree.data);
            if(tree.lChild!=null){
                queue.add(tree.lChild);
            }
            if(tree.rChild!=null){
                queue.add(tree.rChild);
            }
        }
        System.out.println();
    }
    
    
    
    
    求二叉树深度
    public static int depth(BinaryTree root){
        if(root==null){
            return 0;}
        int left = 1;
        int right =1;
        left += depth(root.lChild);
        right += depth(root.rChild);
        return left>right?left:right;
        
    }
    
    方法2:求二叉树深度
    public static int depth2(BinaryTree root,int depth){
        if(root==null){
            return depth;}
        depth++;
        int left = depth2(root.lChild,depth);
        int right = depth2(root.rChild,depth);
        return left>right?left:right;
        
    }
    
    //树的结点数
    public static int nodesize(BinaryTree root,int size){
        if(root==null){
            return size;
        }
        
        size++;
        size = nodesize(root.lChild,size);
        size = nodesize(root.rChild,size);
        return size;
    }
    
    //对称的二叉树主函数
     public static boolean isSymmetrical(BinaryTree pRoot)  
        {  
            if(pRoot==null) return true;  
      
            BinaryTree pRight=pRoot.rChild;  
            BinaryTree pLeft=pRoot.lChild;  
            if(pRight==null && pLeft==null) return true;  
            if(pRight==null || pLeft==null) return false;//不要忘了  
            return isJudge(pRight,pLeft);  
        }  
     
    //对称的二叉树从函数
     public static boolean isJudge(BinaryTree pLroot,BinaryTree pRroot)//递归让它自己比较左右子树,注意退出的条件  
        {  
            if(pLroot==null && pRroot==null) return true;  
            if(pLroot==null || pRroot==null) return false;//不要忘了  
            if(pLroot.data!=pRroot.data) return false;  
      
            BinaryTree pL1=pLroot.lChild;  
            BinaryTree pL2=pLroot.rChild;  
      
            BinaryTree pR1=pRroot.lChild;  
            BinaryTree pR2=pRroot.rChild;  
      
            return isJudge(pL1,pR2) && isJudge(pL2,pR1);  
        }  
    
     
    //按层次遍历(打印某一层,递归)
    public static void PrintNodeAtLevel(BinaryTree root,int level)
    {
        // 空树或层级不合理
        if (null == root || level < 1 )
             return;
     
        if (level==1)
       {
            System.out.print( root.data);
            return;
        }
    
        // 左子树的 level - 1 级
         PrintNodeAtLevel(root.lChild,  level - 1);
    
        // 右子树的 level - 1 级
         PrintNodeAtLevel(root.rChild, level - 1);
     }
    
    public static void PrintNodeAtLevel2(BinaryTree root,int current,int target)
    {
        // 空树或层级不合理
        if (null == root)
             return;
     
        if (current==target-1)
       {
            if(root.lChild!=null)
                System.out.print(root.lChild.data);
            if(root.rChild!=null)
                System.out.print(root.rChild.data);
            return;
        }
        current++;
        // 左子树的 level - 1 级
         PrintNodeAtLevel2(root.lChild,  current,target);
    
        // 右子树的 level - 1 级
         PrintNodeAtLevel2(root.rChild, current,target);
     }
    
    public static void printMidNext(BinaryTree node,BinaryTree parent){
        //是子节点
        if(node.lChild==null&&node.rChild==null){
            if(parent.rChild==null){
                System.out.println(parent.parent.data);
            }
        }
    }
    
    //反转树
    public static void reverse(BinaryTree node){
        if(node==null || (node.lChild==null&&node.rChild==null)){
            return;
        }
        BinaryTree temp = node.lChild;
        node.lChild = node.rChild;
        node.rChild = temp;
        
        reverse(node.lChild);
        reverse(node.rChild);
    }
    
    //二叉树中和为某一值得路径
    
    public static Stack<Integer> stack = new Stack<Integer>();
    public static void sumpath(BinaryTree node, int sum,int target){
        if(node==null)
            return;
        
        //用前序遍历方法,可以首先访问节点,然后将节点入栈,并将数值和之前入栈的节点值相加
        sum += (int)node.data;
        stack.push((int)node.data);
        
        //如果当前之和否满足给定值,判断当前节点是否叶节点,是则打印路径信息
        if(node.lChild==null&&node.rChild==null&&sum==target){
            for(int data:stack){
                System.out.print(data+" ");
            }
            
        }
        
        //判断节点左右孩子是否为空,递归调用
        
        if(node.lChild!=null){
            sumpath(node.lChild, sum, target);
        }
        if(node.rChild!=null){
            sumpath(node.rChild, sum, target);
        }
        
        //在调用完,返回时要将入栈的值出栈(此时栈中节点只到父节点),和变量也要变回调用之前的状态
        
        sum -= (int)node.data;
        stack.pop();
    }

 

转载于:https://www.cnblogs.com/joshsung/p/7464554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值