Java編程求二叉樹的鏡像兩種方法介紹
給出一棵二叉樹,求它的鏡像,如下圖:右邊是二叉樹是左邊二叉樹的鏡像。

仔細分析這兩棵樹的特點,看看能不能總結出求鏡像的步驟。這兩棵樹的根節(jié)點相同,但他們的左右兩個子節(jié)點交換了位置。因此我們不妨先在樹中交換根節(jié)點的兩個子節(jié)點,就得到了下面一幅圖中的第二顆樹
解法1(遞歸)
思路1:如果當前節(jié)點為空,返回,否則交換該節(jié)點的左右節(jié)點,遞歸的對其左右節(jié)點進行交換處理。
/*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;
//交換該節(jié)點指向的左右節(jié)點。
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
//對其左右孩子進行鏡像處理。
mirrorTree(root.left);
mirrorTree(root.right);
}
交換過程如下圖:

交換根節(jié)點的兩個子節(jié)點之后,我們注意到值為10,6的結點的子節(jié)點仍然保持不變,因此我們還需要交換這兩個結點的左右子節(jié)點。交換之后的結果分別為第三課樹和第四顆樹。做完這兩次交換之后,我們已經(jīng)遍歷完所有的非葉子結點。此時交換之后的樹剛好就是原始樹的鏡像。
思路2:如果當前節(jié)點為 null,返回 null ,否則先分別對該節(jié)點的左右孩子進行鏡像處理,然后將該節(jié)點的左指針指向右孩子,右指針指向左孩子,對該節(jié)點進行鏡像處理。
/*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);
//對當前節(jié)點進行鏡像處理。
root.left=right;
root.right=left;
return root;
}
解法2(非遞歸)
思路1:層次遍歷,根節(jié)點不為 null 將根節(jié)點入隊,判斷隊不為空時,節(jié)點出隊,交換該節(jié)點的左右孩子,如果左右孩子不為空,將左右孩子入隊。
public static void mirrorTreeWithQueue(TreeNode root)
{
if(root==null)
return;
//如果樹為 null 直接返回。否則將根節(jié)點入隊列。
Queue<TreeNode> queue= new LinkedList<TreeNode>() ;
queue.add(root);
while(!queue.isEmpty())
{
//隊列不為空時,節(jié)點出隊,交換該節(jié)點的左右子樹。
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:先序遍歷,如果根節(jié)點不為 null 將根節(jié)點入棧,當棧不為 null 出棧,交換左右節(jié)點,如果左右節(jié)點不為 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;
}
總結
以上就是本文關于Java編程求二叉樹的鏡像兩種方法介紹的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。
相關文章
Java線程池submit阻塞獲取結果的實現(xiàn)原理詳解
Java線程池中提交任務運行,通常使用execute()方法就足夠了。那如果想要實現(xiàn)在主線程中阻塞獲取線程池任務運行的結果,該怎么辦呢?本文就來和大家一起討論討論2022-10-10
IDEA調(diào)試技巧條件斷點實現(xiàn)步驟詳解
這篇文章主要介紹了IDEA調(diào)試技巧條件斷點實現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
Spring boot從安裝到交互功能實現(xiàn)零基礎全程詳解
這篇文章主要介紹了Spring boot從安裝到交互功能得實現(xiàn)全程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

