二叉樹的遍歷

二叉樹的定義

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;

    }
發佈了93 篇原創文章 · 獲贊 15 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章