二叉树的遍历

二叉树的定义

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
    public TreeNode() {
        // TODO Auto-generated constructor stub
    }
}

1,重建

public static TreeNode buildTree(int[] preOrder,
              int start,int[] inOrder,int end,int length){

         //参数验证
        if(preOrder==null||preOrder.length==0
        ||inOrder==null||inOrder.length==0||length<=0){

            return null;            
        }

        //建立子树根节点
        int value=preOrder[start];
        TreeNode root=new TreeNode(value);

        //递归终止条件:子树只有一个节点
        if(length==1){
            return root;
        }

        //分拆子树的左子树和右子树
        int i=0;
        while(i<length){
            if(value==inOrder[end-i]){
                break;
            }
            i++;            
        }


  //建立子树的左子树
  root.left=buildTree(preOrder,start+1,inOrder,end-1-i,length-1-i);
  root.right=buildTree(preOrder,start+length-i,inOrder,end,i);

  return root;
}

2, 前序遍历

   //前序遍历     递归    
    public static void preorderTraversal(TreeNode root){

            if(root==null) return ;

            System.out.print(root.val+"  ");
            inorderTraversal(root.left);        
            inorderTraversal(root.right);                               
    }
    //前序遍历    非递归
    public static List<TreeNode> preorderTraversal2(TreeNode root){

         List<TreeNode> tList=new ArrayList<TreeNode>();
         Stack<TreeNode> tStack=new Stack<TreeNode>();

         tStack.push(root);
         while(!tStack.isEmpty()){
             TreeNode p=tStack.pop();
             tList.add(p);
             System.out.print(p.val+" ");

             if(p.right!=null) tStack.push(p.right);
             if(p.left!=null)  tStack.push(p.left);
         } 

         return tList;

    }

3,中序遍历

    //中序遍历     递归    
    public static void inorderTraversal(TreeNode root){

            if(root==null) return ;

            inorderTraversal(root.left);
            System.out.print(root.val+"  ");        
            inorderTraversal(root.right);                               
    }
    //中序遍历  非递归
    public static List<TreeNode>  inorderTraversal2(TreeNode root){
         List<TreeNode> tList=new ArrayList<TreeNode>();
         Stack<TreeNode> tStack=new Stack<TreeNode>();

         TreeNode p=root;
         while(p!=null || !tStack.isEmpty()){
             if(p!=null){
                 tStack.push(p);
                 p=p.left;
             }
             else{
                 p=tStack.pop();
                 tList.add(p);
                 p=p.right;
             }
         }       
         return tList;
    }

4,后序遍历

   //后序遍历     递归    
    public static void postorderTraversal(TreeNode root){

            if(root==null) return ;

            inorderTraversal(root.left);        
            inorderTraversal(root.right);
            System.out.print(root.val+"  ");
    }

5,层序遍历

    //层序遍历
    public static void levelTravel(TreeNode root){
        if(root==null) return;
        Queue<TreeNode> q=new LinkedList<TreeNode>();
        q.add(root);
        while(!q.isEmpty()){
            TreeNode p=q.poll();
            System.out.println(p.val);
            if(p.left!=null) q.add(p.left);
            if(p.right!=null) q.add(p.right);

        }

    }
    //层序遍历,返回List<List<Integer>> 
    public List<List<Integer>> levelOrder(TreeNode root){

        List<List<Integer>> list=new LinkedList<List<Integer>>();
        if(root==null ) return list;

        //创建一个队列,用于存放所有节点
        Queue<TreeNode> currentLevel=new LinkedList<TreeNode>();
        currentLevel.add(root);
        while(!currentLevel.isEmpty()){
            //创建一个List记录当前层所有节点值
            List<Integer> currentList=new LinkedList<Integer>();
            for(int i=0;i<currentLevel.size();i++){
                TreeNode currentNode=currentLevel.poll();
                currentList.add(currentNode.val);
                if(currentNode.left!=null)
                    currentLevel.add(currentNode.left);
                if(currentNode.right!=null)
                    currentLevel.add(currentNode.right);
            }
            list.add(currentList);
        }

        return list;
    }

6,深度

//二叉树深度
    public static int depthOfBinaryTree(TreeNode root){
          if(root==null)
              return 0;
          return Math.max(depthOfBinaryTree(root.left), depthOfBinaryTree(root.right))+1;
    }

7,平衡二叉树的判定

    //是否是平衡二叉树
    public static boolean  isBalance(TreeNode root){

          if(root==null)
              return true;
          if(Math.abs(depthOfBinaryTree(root.left)-depthOfBinaryTree(root.right))>1){
              return false;
          }

          return isBalance(root.left)&&isBalance(root.right);       
    }

8,路径和为某特定值

public boolean hasPathSum(TreeNode root, int sum) {

        if(root==null)
            return false;
        if(root.left==null && root.right==null && root.val==sum)
            return true;
        return hasPathSum(root.left,sum-root.val) ||hasPathSum(root.right,sum-root.val);

    }

9,到叶子节点的最短路径。

    public int minDepth(TreeNode root) {

        if(root==null) return 0;

        int left=minDepth(root.left);
        int right=minDepth(root.right);

        if(left==0 && right==0) 
            return 1; 

        if(left==0) 
            left=Integer.MAX_VALUE;
        if(right==0) 
            right=Integer.MAX_VALUE;

        return Math.min(left, right)+1;

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值