Merge pull request #364 from luocaodan/patch-6

Update 剑指 offer 题解.md
This commit is contained in:
郑永川 2018-08-09 22:28:19 +08:00 committed by GitHub
commit 7de885099a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1298,6 +1298,7 @@ private boolean isSubtreeWithRoot(TreeNode root1, TreeNode root2)
## 解题思路 ## 解题思路
### 递归
```java ```java
public void Mirror(TreeNode root) 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<TreeNode> 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 对称的二叉树 # 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) [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)