筆試面試演算法經典--二叉樹的鏡像-遞迴與非遞迴實現(Java)__編碼

來源:互聯網
上載者:User

給出一棵二叉樹,求它的鏡像,如下圖:右邊是二叉樹是左邊二叉樹的鏡像。

解法1(遞迴)

思路1:如果當前節點為空白,返回,否則交換該節點的左右節點,遞迴的對其左右節點進行交換處理。

/*class TreeNode{    int val;    TreeNode left=null;     TreeNode right=null;    public TreeNode(int val) {        this.val = val;    }}*/public static void mirrorTree(TreeNode root)    {        if(root==null)            return;        //交換該節點指向的左右節點。        TreeNode temp=root.left;        root.left=root.right;        root.right=temp;        //對其左右孩子進行鏡像處理。        mirrorTree(root.left);        mirrorTree(root.right);    }

交換過程如下圖:

思路2:如果當前節點為 null,返回 null ,否則先分別對該節點的左右孩子進行鏡像處理,然後將該節點的左指標指向右孩子,右指標指向左孩子,對該節點進行鏡像處理。

/*class TreeNode{    int val;    TreeNode left=null;     TreeNode right=null;    public TreeNode(int val) {        this.val = val;    }}*/public static TreeNode mirrorTree1(TreeNode root)    {        if(root==null)            return null;        //對左右孩子鏡像處理        TreeNode left=mirrorTree1(root.left);        TreeNode right=mirrorTree1(root.right);        //對當前節點進行鏡像處理。        root.left=right;        root.right=left;        return root;            }
解法2(非遞迴)

思路1:層次遍曆,根節點不為 null 將根節點入隊,判斷隊不為空白時,節點出隊,交換該節點的左右孩子,如果左右孩子不為空白,將左右孩子入隊。

    public static void mirrorTreeWithQueue(TreeNode root)    {        if(root==null)            return;        //如果樹為 null 直接返回。否則將根節點入隊列。        Queue<TreeNode> queue= new LinkedList<TreeNode>() ;        queue.add(root);        while(!queue.isEmpty())        {           //隊列不為空白時,節點出隊,交換該節點的左右子樹。            TreeNode root1=queue.poll();            /*TreeNode left,right;            left=root1.left;            right=root1.right;            root1.right=left;            root1.left=right;            */            Swap(root);            if(root1.right!=null)            {                queue.add(root1.right);                //如果左子樹不為 null 入隊            }            if(root1.left!=null)            {                queue.add(root1.left);                //如果右子樹不為 null 入隊。            }         }    }    public static void Swap(TreeNode root)    {        TreeNode temp;        temp=root.right;        root.right=root.left;        root.left=temp;    }

思路2:先序遍曆,如果根節點不為 null 將根節點入棧,當棧不為 null 出棧,交換左右節點,如果左右節點不為 null 入棧。

public static void mirrorTreeWithStack(TreeNode root)    {           if(root==null)            return;        Stack<TreeNode> stack=new Stack<TreeNode>();        stack.push(root);        while(!stack.isEmpty())        {        //當棧不為 null 時出棧,交換左右子樹。            TreeNode root1=stack.pop();            /*TreeNode left,right;            left=root1.left;            right=root1.right;            root1.right=left;            root1.left=right;*/            Swap(root);            if(root1.right!=null)            {            //右子樹不為 null 入棧                stack.push(root1.right);            }            if(root1.left!=null)            {            //左子樹不為 null 入棧                stack.push(root1.left);            }        }    }public static void Swap(TreeNode root)    {        TreeNode temp;        temp=root.right;        root.right=root.left;        root.left=temp;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.