Problem:

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

For example:
Given a binary tree {1,2,3,4,5},

    1
/ \
2 3
/ \
4 5

return the root of the binary tree [4,5,2,#,#,3,1].

   4
/ \
5 2
/ \
3 1

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Wrong Solution:

public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null)
return root;
TreeNode upsided_left = upsideDownBinaryTree(root.left);
TreeNode upsided_right = upsideDownBinaryTree(root.right);
if (upsided_left == null)
return root;
root.left = null;
root.right = null;
TreeNode right_most = upsided_left;
if (right_most.right != null)
right_most = right_most.right;
right_most.left = upsided_right;
right_most.right = root;
return upsided_left;
}
}

Mistakes Analysis:

Mistake analysis:
Input:
[1,2,null,3,null,4]
Output:
[4,null,3,null,1]
Expected:
[4,null,3,null,2,null,1] The above code is complicated and wrong. Cause I don't throughly understand the logic behind this problem.
I even try to find the right insert position at the upsided result, which is a indicator of wrong. Actually the idea is really not hard.
For a tree, we must sure,
1. right child must be a leaf node or empty.
2. the current left subtree would become the new root (see it as a single node). I understand the above two important points.
But I miss this one.
3. iff the left subtree is not a single node (the root of the left sub-tree would become its upsidedtree's rightmost node, where we should attach the root as left child and root as right child)
Very important!!!! Thus above solution is complex and wrong.
fix 1: TreeNode upsided_right = upsideDownBinaryTree(root.right);
Since right child must be a leaf or empty, there is no need to perform upsideDownBinaryTree over it. fix 2: Don't try to search to rightmost node manually, the left child of current tree has already been turned into the right most node.
TreeNode right_most = upsided_left;
if (right_most.right != null)
right_most = right_most.right;
right_most.left = upsided_right;
right_most.right = root; root pointer has not been updated after "upsideDownBinaryTree(root.left)", root.left still point to it's left child before upsided. (root's informaiton has not been changed yet!!!)
root.left.left = root.right;
root.left.right = root;

You should also pay attention to the base case of this solution.
It could be null pointer or leaf node
null pointer : root == null
leaf node : root.left == null && root.right == null
In case we need to continue to search at next level when this only left child.

Solution:

public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null || root.left == null && root.right == null)
return root;
TreeNode newRoot = upsideDownBinaryTree(root.left);
root.left.left = root.right;
root.left.right = root; root.left = null;
root.right = null; return newRoot;
}
}

[LeetCode#156] Binary Tree Upside Down的更多相关文章

  1. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  2. [leetcode]156.Binary Tree Upside Down颠倒二叉树

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  3. [LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  4. 【LeetCode】Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  5. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  6. [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  7. 156. Binary Tree Upside Down

    题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

  8. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

  9. [LC] 156. Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

随机推荐

  1. 响应式框架中,table表头自动换行的解决办法

    最近在用bootstrap开发网站,在处理一张table的时候发现,通过PC端查看样式正常,在手机上查看时,因为屏幕小,表格被压缩的厉害,表头和数据变形如下图 后来网上找了一下,发现一个好用的CSS属 ...

  2. 实现输出h264直播流的rtmp服务器 flash直播服务器

    http://www.cnblogs.com/haibindev/archive/2012/04/16/2450989.html 实现输出h264直播流的rtmp服务器 RTMP(Real Time ...

  3. DLL使用总结

    最近项目中使用到了DLL,因此就把最近一段时间的学习总结一下,以备不时之需. 一.相关概念 1.动态链接库 自从微软推出第一个版本的Windows操作系统以来,动态链接库(DLL)一直是Windows ...

  4. 关于IOS网络通信的学习

    最近由于需要在看关于网络通信方面的知识,所以在网上找了找关于网络解释方面的知识.找了半天没有找到一篇能详细解释通讯流程的,心里忍不住就万马奔腾了.没办法,谁让自己想学呢!于是又找了找,觉得没有满意的. ...

  5. scrapy, 自带命令行调用工具.

    #-*- coding:utf-8 -*- from scrapy import cmdline cmdline.execute("scrapy crawl dmoz".split ...

  6. 03_JqueryAjax_异步请求Servlet

    [Ajax 简述] jquery对Ajax提供了更方便的代码:$ajax({ops})来发送异步请求. 首先说一个Ajax的特性,它是永安里发送异步请求,请求的是服务器,但不会刷新页面. 例如在注册功 ...

  7. Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)

    Dough.h #ifndef _DOUGH_H #define _DOUGH_H class Dough { }; #endif ThinCrustDough.h #ifndef _THIN_CRU ...

  8. Docker命令使用详解

    其中<>括起来的参数为必选, []括起来为可选 docker -exec -i -t 3f407013d8c0 /bin/bash    进入容器 docker version查看dock ...

  9. express开发实例

    express获取参数有三种方法:官网介绍如下 Checks route params (req.params), ex: /user/:id Checks query string params ( ...

  10. linux 下C语言编程库文件处理与Makefile编写

    做开发快3年了,在linux下编译安装软件算是家常便饭了.就拿gcc来说,都有不下10次了,可基本每次都会碰到些奇奇怪怪的问题.看来还是像vs.codeblocks这样的ide把人弄蠢了.便下定决心一 ...