问题描述
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
原问题链接:https://leetcode.com/problems/binary-tree-upside-down/
问题分析
对于二叉树的问题,我们比较容易想到的是看能否有什么递归的方式来解决这个问题。这里我们可以利用这么一个思路:在某个节点的时候,我们把它的左子树颠倒,在颠倒完成后,原来那个左子树的左右子节点分别指向原来的根节点和原来的右兄弟节点。这样,在实现的时候,我们需要保存有一个当前子节点的上一级节点,在返回的时候设置。
详细的代码实现如下:
public class Solution { public TreeNode upsideDownBinaryTree(TreeNode root) { if(root == null) return null; TreeNode parent = root, left = root.left, right = root.right; if(left != null) { TreeNode ret = upsideDownBinaryTree(left); left.left = right; left.right = parent; return ret; } return root; } }
非递归方式实现
基于上述递归实现的代码也可以转换成非递归的迭代实现方式,这种方式的实现如下:
public TreeNode upsideDownBinaryTree(TreeNode root) { TreeNode node = root, parent = null, right = null; while (node != null) { TreeNode left = node.left; node.left = right; right = node.right; node.right = parent; parent = node; node = left; } return parent; }
总结
对二叉树的上下颠倒转换比较复杂,需要结合递归和一些树的关系进行转换。最好结合树的结构图进行详细的转换推导。
参考材料
http://bangbingsyb.blogspot.jp/2014/11/leetcode-binary-tree-upside-down.html
http://blog.csdn.net/whuwangyi/article/details/43186045
相关推荐
leetcode的题目:Balanced Binary Tree
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9题: ...
leetcode11 top 1. 位运算 LeetCode191 : 二进制位1的个数 LeetCode338 : 比特位运算 2. 字典树 LeetCode209 : 实现一个Trie结构 LeetCode79 : 单词搜索(判断单词是否出现在给定的网格中) LeetCode212 : 单词搜索II...
LeetCode::laptop:LeetCode解决方案
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。 LeetCode 1 : 二和 (46_Easy) LeetCode 2 : 两个数字相加 (96_Medium) LeetCode 3 : 无重复字符的最长子串 (214_Medium) LeetCode 4 : 两个...
LeetCode 在LeetCode和其他编码平台上解决的问题的集合
leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode 面试题。 :receipt: 目录 统计 值 AC 的...
Leetcode:Leetcode提交
leetcode 分类 LeetCode :bouquet::bouquet::bouquet: 介绍 leetcode 题解,Issues 会记录 leetcode 解题之路,并使用 label 进行了分类。 目录 链表
LeetCode 101:和你一起你轻松刷题(C++)
:fire: Leetcode :fire: 实践使完美 :party_popper: 开玩笑的单元测试 :sparkles: 简单的代码 :artist_palette: 可读代码 入门指南 git clone https: //github.com/tangweikun/leetcode.git cd leetcode npm ...
idea中leetcode插件Rust 中的 LeetCode 解决方案 怎么跑?...,所有解决方案代码都在leetcode::leetcode::editor::en并重用于leetcode 。 它有一个全局结构Solution ,所有解决方案条目都在其中实现。
leetcode:leetcode刷题
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
Leetcode:LeetCode解题代码
LeetCode:LeetCode的代码
LeetCode:LeetCode的注释
leetcode:LeetCode问题