From 9a1a7ef04780063398576266ee677f1ab86d0068 Mon Sep 17 00:00:00 2001 From: luocaodan Date: Wed, 8 Aug 2018 22:56:45 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87=20offer=20=E9=A2=98?= =?UTF-8?q?=E8=A7=A3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...214\207 offer \351\242\230\350\247\243.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/notes/\345\211\221\346\214\207 offer \351\242\230\350\247\243.md" "b/notes/\345\211\221\346\214\207 offer \351\242\230\350\247\243.md" index 47d5d94e..5d9adbea 100644 --- "a/notes/\345\211\221\346\214\207 offer \351\242\230\350\247\243.md" +++ "b/notes/\345\211\221\346\214\207 offer \351\242\230\350\247\243.md" @@ -1298,6 +1298,7 @@ private boolean isSubtreeWithRoot(TreeNode root1, TreeNode root2) ## 解题思路 +### 递归 ```java public void Mirror(TreeNode root) { @@ -1316,6 +1317,28 @@ private void swap(TreeNode root) } ``` +### 迭代 +```java +public void Mirror(TreeNode root) { + if (root == null) + return; + Stack stack = new Stack<>(); + stack.push(root); + while (!stack.isEmpty()) { + TreeNode p = stack.pop(); + if (p.left == null && p.right == null) + continue; + TreeNode left = p.left; + p.left = p.right; + p.right = left; + if (p.left != null) + stack.push(p.left); + if (p.right != null) + stack.push(p.right); + } +} +``` + # 28 对称的二叉树 [NowCder](https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb?tpId=13&tqId=11211&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) -- GitLab