作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode-cn.com/problems/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.

Example:

Input: [1,2,3,4,5]

    1
/ \
2 3
/ \
4 5 Output: return the root of the binary tree [4,5,2,#,#,3,1] 4
/ \
5 2
/ \
3 1

题目大意

给定一个二叉树,其中所有的右节点要么是具有兄弟节点(拥有相同父节点的左节点)的叶节点,要么为空,将此二叉树上下翻转并将它变成一棵树, 原来的右节点将转换成左叶节点。返回新的根。

解题方法

递归

树的做法一般都可以递归和非递归做法。递归做法说明如下:

把一棵树右旋,规律是:左孩子变成了根,右孩子变成了左子树,根节点变成了右子树。

我们先保存下来未旋转之前的左右孩子,然后对左子树递归做上面的操作,注意右孩子是叶子节点或者为空,所以不用对右孩子递归。另外由于根节点变成了右子树,需要把它的两个孩子设置为空。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if (!root || !root->left) return root;
TreeNode* l = root->left;
TreeNode* r = root->right;
TreeNode* newRoot = upsideDownBinaryTree(root->left);
l->left = r;
l->right = root;
root->left = nullptr;
root->right = nullptr;
return newRoot;
}
};

迭代

迭代的做法比较绕。

pre保存新树的根节点;
cur保存每个节点,每次向左下方移动;
next保存cur->next;
temp保存cur->right;

所以核心语句就是修改cur->left和cur->right两句,其余语句都是在移动指针,要注意的是保存和移动的顺序不能和修改相冲突。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
TreeNode* cur = root, *pre = nullptr, *temp = nullptr, *nxt = nullptr;
while (cur) {
nxt = cur->left;
cur->left = temp;
temp = cur->right;
cur->right = pre;
pre = cur;
cur = nxt;
}
return pre;
}
};

日期

2019 年 9 月 17 日 —— 听了hulu宣讲会,觉得hulu的压力不大

【LeetCode】156. Binary Tree Upside Down 解题报告(C++)的更多相关文章

  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

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

  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] 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 ...

  5. 【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 ...

  6. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

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

  7. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

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

  8. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

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

  9. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

随机推荐

  1. windows下的python安装pysam报错

    安装pysam时报错: 指定版本仍报错: 使用pysam-win安装: 但是import时不行: 貌似pysam在windows下难以正常配置,还是在Linux中用吧. https://www.jia ...

  2. shell编程100列

    1.编写hello world脚本 #!/bin/bash# 编写hello world脚本 echo "Hello World!"2.通过位置变量创建 Linux 系统账户及密码 ...

  3. Python基础之基本运算符

    目录 1. 算数运算符 2. 比较运算符 3. 赋值运算符 4. 逻辑运算符 5. 身份运算 6. 运算符优先级 1. 算数运算符 常用算术运算符使用方法如下: x = 5 y = 2 a = x + ...

  4. Python—安装跟爬虫相关的包

    舆情爬虫分析:硬件:   4台服务器,分别放redis.python爬虫.mysql和 kafka四大板块.软件:1. mysql2. redis    #leap1  /usr/bin/redis- ...

  5. 48-Merge Sorted Array

    $88. Merge Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 98885 Total Submis ...

  6. SQL- case when then else end 用法经验总结

    对case when 的理解总结: 1.then和else后,只能写一条输出语句且输出结果就是新生成列的值;when 后的条件判断可以有多条,且可以多个字段联合判断:end 后的输出也可以有多条,但必 ...

  7. git放弃修改,强制覆盖本地代码

    1.git fetch --all  //从远程拉取最新的代码 不merge 2.git reset --hard origin/develop  //使用指定分支的代码(此处develop)强制覆盖 ...

  8. mongoDB整个文件夹拷贝备份还原的坑

    现网有一个mongoDB数据库需要搬迁到新服务器,开发那边的要求是先搬迁现在的数据库过去,然后剩下的以后他们用程序同步. 数据库大楷20G左右,现网是主备仲裁的,停掉备点,拷贝了全部文件. 新服务器也 ...

  9. 在idea的java开发中字符串length()方法获取长度与赋值不符的问题

    最近在开发中用到length()方法获取中文字符串的长度,发现获得的长度与实际不符.比如个String类型赋值为"中",但获取长度却是2. 这让我百思不得其解,后来突然想起来我在研 ...

  10. volatile原理和应用场景

    volatile是java语言中的一个关键字,常用于并发编程,有两个重要的特点:具有可见性,java虚拟机实现会为其满足Happens before原则;不具备原子性.用法是修饰变量,如:volati ...