Invert Binary Tree
2015-08-19 14:22
309 查看
Invert a binary tree.
to
使用队列来存储节点,交互节点的左右子树。
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
使用队列来存储节点,交互节点的左右子树。
public TreeNode invertTree(TreeNode root) { if(root==null) return null; Queue<TreeNode> qe=new LinkedList<TreeNode>(); qe.add(root); while(!qe.isEmpty()){ TreeNode node=qe.poll(); if(node.left!=null) qe.add(node.left); if(node.right!=null) qe.add(node.right); TreeNode t=node.left; node.left=node.right; node.right=t; } return root; }
相关文章推荐
- 13款最好的开源Linux操作系统
- HDU 5400 Arithmetic Sequence
- glass choice and eye protection
- android帧动画,移动位置,缩放,改变透明度等动画讲解
- hdu 5400 Arithmetic Sequence 2015多校联合训练赛#9 枚举
- C++ Web Programming
- android帧动画,移动位置,缩放,改变透明度等动画讲解
- oc 类型判断
- springmvc使用和经验总结(长沙师说网络科技有限公司)
- iconv
- PAT 1046. Shortest Distance (20)
- Redis集群进阶-使用集群
- 学习计划
- 可伸缩系统的架构经验乱谈
- ubuntu 14.04 下找不到命令,路径出错
- 【2015上半年总结】js开源组件开发系列索引
- 聚合支付概念
- Java中Math类的几种常用方法
- 抽象类的相关问题
- Delete from join 用法