Binary Tree Upside Down

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.

分析:

  自底向上的右旋,用DFS搜到最左下角的节点,然后依次进行处理

代码:

class Solution {
private:
TreeNode *newRoot;
public:
void dfs(TreeNode* node) {
if(!node->left) {
newRoot = node;
return;
}
dfs(node->left);
node->left->left = node->right;
node->left->right = node;
return;
}
TreeNode* upsideDown(TreeNode* root) {
if(root)
dfs(root);
return newRoot;
}
};

[Locked] 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】Binary Tree Upside Down

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

  3. [LeetCode] 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. Binary Tree Upside Down

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

  5. LeetCode Binary Tree Upside Down

    原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...

  6. 156. Binary Tree Upside Down

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

  7. [LeetCode#156] Binary Tree Upside Down

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

  8. [Swift]LeetCode156.二叉树的上下颠倒 $ Binary Tree Upside Down

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

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

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

随机推荐

  1. Registry 类

    提供表示 Windows 注册表中的根项的 RegistryKey 对象,并提供访问项/值对的 static 方法. 继承层次结构 System.Object   Microsoft.Win32.Re ...

  2. C#winform程序自定义鼠标样式

    public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...

  3. Nhibernate 智能提示 以及其他类库智能提示

    Nhibernate 的智能提示 Nhibernate.dll 放到以下路径 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewo ...

  4. opencv有关错误及解决办法

    1.载入图片时内存溢出情况,如图: 分析及解决办法:因为载入的图片太大,导致内存溢出.所以更换小一点的图片就行了. 2.

  5. javascript——事件处理

    <script type="text/javascript"> function EventUtil() { var _self = this; ///添加事件 var ...

  6. 第八篇、SVN在Mac上使用

    Mac自带svn软件 1.创建目录 svn-repository/source-code 2.svnadmin create /Users/liaokailin/svn-repository/sour ...

  7. CSAPP LAB: Buffer Overflow

    这是CSAPP官网上的著名实验,通过注入汇编代码实现堆栈溢出攻击.实验材料可到我的github仓库 https://github.com/Cheukyin/CSAPP-LAB/ 选择buffer-ov ...

  8. 利用正则表达式,给Json字段加引号

    { scheme: [ { query: [ [{ id: 'stdNumber', title: "标准号", compareType: 2 }], [{ id: 'CnName ...

  9. <select>与<datalist>的区别

    size:下拉框中每次出现选项的个数 multiple:可以一次性选多个选项: disabled:时下拉框不可用,无法点击选项​ list:它的值应于id的值对应 datalist要与input标签一 ...

  10. C++ 类的前向声明

    前向声明 在计算机程序设计中, 前向声明是指声明标识符(表示编程的实体,如数据类型.变量.函数)时还没有给出完整的定义.即可以声明一个类而不定义它,只声明类但不知道类的成员变量.函数等具体细节. 如: ...