1. 程式人生 > 其它 >226.翻轉二叉樹

226.翻轉二叉樹

翻轉一棵二叉樹。

示例:

針對二叉樹的問題,解題之前一定要想清楚究竟是前中後序遍歷,還是層序遍歷

這道題目使用前序遍歷和後序遍歷都可以,唯獨中序遍歷不方便,因為中序遍歷會把某些節點的左右孩子翻轉了兩次!建議拿紙畫一畫,就理解了

那麼層序遍歷可以不可以呢?依然可以的!只要把每一個節點的左右孩子翻轉一下的遍歷方式都是可以的!

1、前序、後序遍歷遞迴實現

 public TreeNode invertTree(TreeNode root) {
         postInvert(root);
         return root;

    }

    private void postInvert(TreeNode root){
        if(root==null){
            return;
        }
         //swap(root);  
        postInvert(root.left);
        postInvert(root.right);
        swap(root);
       
    }

    private void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }

  

  2、層序遍歷

public TreeNode invertTree(TreeNode root) {
         levelOrder(root);
         return root;

    }

    private void levelOrder(TreeNode root){
        if(root==null){
            return;
        }

        TreeNode cur=root;
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.add(cur);
        while(!queue.isEmpty()){// cur l r .l r.l r
            cur=queue.pop();
            //System.out.println(cur.val);
            swap(cur);
            if(cur.left!=null){
                queue.add(cur.left);
            }
            if(cur.right!=null){
                queue.add(cur.right);
            }
        }
         
       
    }

    private void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }

  

3、前序遍歷非遞迴實現

  public TreeNode invertTree(TreeNode root) {
         preOrder(root);
         return root;

    }

    private void preOrder(TreeNode root){
        if(root==null){
            return;
        }

        //cur l r. stack  cur r l
        TreeNode cur=root;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(cur);
        while(!stack.isEmpty()){
            cur=stack.pop();
            swap(cur);
            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }

         
       
    }

    private void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }

  4、後序遍歷非遞迴實現

public TreeNode invertTree(TreeNode root) {
         postOrder(root);
         return root;

    }

    private void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        //l r cur. stack1 cur l r. stack cur r l
        TreeNode cur=root;
        Stack<TreeNode> stack1=new Stack<>();
        Stack<TreeNode> stack2=new Stack<>();
        stack1.push(cur);
        while(!stack1.isEmpty()){
            cur=stack1.pop();
            stack2.push(cur);
            if(cur.left!=null){
                stack1.push(cur.left);
            }
            if(cur.right!=null){
                stack1.push(cur.right);
            }
        }
        while(!stack2.isEmpty()){
            swap(stack2.pop());
        }

    }

    private void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }