class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode current=queue.poll();
TreeNode tmp=current.left;
current.left=current.right;
current.right=tmp;
if(current.left!=null)
queue.add(current.left);
if(current.right!=null)
queue.add(current.right);
}
return root;
}
}
leetcode:16.翻转二叉树 简单
最新推荐文章于 2024-05-21 18:12:47 发布