Invert Binary Tree
2015-12-07 15:30
274 查看
Invert a binary tree:
to
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1 简单递归实现,调换左右子树,子树的所有子树结构一并调换了顺序。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode invertTree(TreeNode root) { TreeNode temp; if(root == null) { return null; } temp = root.left; root.left = root.right; root.right = temp; invertTree(root.right); invertTree(root.left); return root; } }
相关文章推荐
- Redis缓存全自动安装shell脚本
- 1052. Linked List Sorting (25)【链表+排序】——PAT (Advanced Level) Practise
- 2015-12-7 15:29:40
- uva 1589 xiangqi
- Mac 可设置环境变量的位置、查看和添加PATH环境变量
- 详细讲述python自省函数getattr的用法
- The request sent by the client was syntactically incorrect ()问题
- ETL工作流缓慢原因查找方法
- automake manul
- 工厂模式:封装对象的创建(二、多态工厂)
- Bitmap的简单保存
- Forward Algorithm
- 各种刷题网址
- Linux串口编程详解
- Super Ugly Number
- artDialog包---html和js完成弹出窗口和跳转
- 堆和栈的区别
- butterKnifeZelezny生成前设置m
- maven多模块使用,父模块(modules使用,package替pom),子模块(parent使用)
- (Android)五分钟学会PopupWindow