LeetCode-Binary Tree Upside Down
2015-10-21 08:12
309 查看
right child is leaf or null 保证了可以flip 就是说right node不可能再有child node
每个node变成自己left child的 right node 自己的right child变成left child的left child
递归 记得要先把左边先做一遍再移动root层的指针 同时root的left right point 要set null
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if ( root == null || root.left == null )
return root;
TreeNode res = upsideDownBinaryTree ( root.left );
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return res;
}
}
每个node变成自己left child的 right node 自己的right child变成left child的left child
递归 记得要先把左边先做一遍再移动root层的指针 同时root的left right point 要set null
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if ( root == null || root.left == null )
return root;
TreeNode res = upsideDownBinaryTree ( root.left );
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return res;
}
}
相关文章推荐
- Ansible PlayBook的原理和实现
- 神经网络之Hebb学习规则
- Android之AlarmManager
- (笔记)Linux下的ioctl()函数详解
- SpringMVC访问资源文件
- 鼠标拖动
- 【bzoj2072】 POI2004MOS dp+贪心
- Vijos1845 花匠
- [Axure教程]0001.新手入门基础
- SQL Server中的事务日志管理(3/9):事务日志,备份与恢复
- java ==,hashCode(),equals(),
- 月薪3万的程序员都避开了哪些坑?
- SpringMVC前端控制器-----DispatcherServlet
- Ubuntu下,查看以及修改PATH的方法
- noip
- LintCode "Triangle Count"
- programming-challenges Chopsticks (111107) 题解
- Google将Material Design带到CSS、HTML与JavaScript上
- powershell常用
- Step7中有关时间和定时器的使用和例程1