作者: 负雪明烛
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. UE4之Slate: SImage

    概述 距离上次记录<UE4之Slate:纯C++工程配置>后已经好长时间了: 这个随笔来记录并分享一下SImage控件的使用,以在屏幕上显示一张图片: 目标 通过SImage控件的展示,学 ...

  2. 学习java 7.12

    学习内容: File是文件和目录路径名的抽象表示,File封装的不是一个真正存在的文件,仅仅是一个路径名 File类的方法 绝对目录和相对目录的区别 字节流 使用字节输出流写数据的步骤 : 创建字节输 ...

  3. A Child's History of England.46

    As, one hundred years before, the servile [卑躬屈膝的~serve] followers of the Court had abandoned the Con ...

  4. HDFS【Java API操作】

    通过java的api对hdfs的资源进行操作 代码:上传.下载.删除.移动/修改.文件详情.判断目录or文件.IO流操作上传/下载 package com.atguigu.hdfsdemo; impo ...

  5. 零基础学习java------26--------获取省访问量的top3,APP版本数据分析,事务,json,json字符串与对象间的相互转换,求电影平均分

    一. day23中的ip,url案例(前面答案错了) 思路分析: 1.创建javabean,用来存储ip.txt各字段的信息 2. 创建java工具类,封装相应的方法 (1) 加载读取ip.txt文档 ...

  6. Linux 下使用rtcwake实现定时休眠和唤醒设备

    查看是否安装rtcwake whereis rtcwake rtcwake: /usr/sbin/rtcwake /usr/share/man/man8/rtcwake.8.gz 查看rtcwake帮 ...

  7. lucene索引的增、删、改

    package com.hope.lucene;import org.apache.lucene.document.Document;import org.apache.lucene.document ...

  8. 什么是git?

    目录 一.简介 Git与SVN 区别 一.简介 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核 ...

  9. JavaWeb的三大作用域

    三大作用域描述 名称 类型 描述 request HttpServletRequest 将数据放在请求作用域中,在一次请求中实现数据的共享,比如请求转发 session HttpSession 将数据 ...

  10. Java值引用和对象引用区别Demo

    转自:http://blog.csdn.net/gundsoul/article/details/4927404 以前就知道JAVA对象分对象引用和值引用,并且还知道8种基础数据类型,即引用时是值引用 ...