您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: