mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update226.翻转二叉树,简化C#代码
This commit is contained in:
@ -1019,24 +1019,12 @@ public class Solution {
|
|||||||
//迭代
|
//迭代
|
||||||
public class Solution {
|
public class Solution {
|
||||||
public TreeNode InvertTree(TreeNode root) {
|
public TreeNode InvertTree(TreeNode root) {
|
||||||
if (root == null) return null;
|
if(root == null) return root;
|
||||||
Stack<TreeNode> stack=new Stack<TreeNode>();
|
(root.left,root.right) = (root.right, root.left);
|
||||||
stack.Push(root);
|
InvertTree(root.left);
|
||||||
while(stack.Count>0)
|
InvertTree(root.right);
|
||||||
{
|
|
||||||
TreeNode node = stack.Pop();
|
|
||||||
swap(node);
|
|
||||||
if(node.right!=null) stack.Push(node.right);
|
|
||||||
if(node.left!=null) stack.Push(node.left);
|
|
||||||
}
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void swap(TreeNode node) {
|
|
||||||
TreeNode temp = node.left;
|
|
||||||
node.left = node.right;
|
|
||||||
node.right = temp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user