[Locked] Binary Tree Upside Down
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的更多相关文章
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- 【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 ...
- [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 ...
- Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- LeetCode Binary Tree Upside Down
原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- [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 ...
- [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 ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
随机推荐
- 认识k_BackingField【转】
事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...
- Sql2008中使用DataTable作为存储过程的参数
使用DataTable作为存储过程的参数 最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...
- TabHost理解与使用
一.继承关系 java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ ...
- Android端上传图片到后台,存储到数据库中 详细代码
首先点击头像弹出popwindow,点击相册,相机,调用手机自带的裁剪功能,然后异步任务类访问服务器,上传头像,保存到数据库中, 下面写出popwindow的代码 //设置popwindow publ ...
- 【原创】Git版本控制器的基本使用
关于git Git,是一个分布式版本控制软件.最初本是为了更好的管理Linux内核开发而被林纳斯·托瓦兹开发,后来因为项目开发中版本控制的强烈需求,而git也日趋成熟,最终成为了一个独立的版本控制软件 ...
- 【HDU4366】【DFS序+分块】Successor
Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...
- zTree异步生成数据时无法获取到子节点的选中状态
最近在项目中遇到一个问题,需求如下: 根据选中不同的人员(ID)向后台发送ajax请求,通过返回的数据来生成该人员的权限访问树,该树目录最少为3级目录,在生成的时候会自动勾选上次保存过的选中状态,点击 ...
- sphinx(coreseek)——1、增量索引
首先介绍一下 CoreSeek/Sphinx的发布包 indexer: 用于创建全文索引; search: 一个简单的命令行(CLI) 的测试程序,用于测试全文索引; search ...
- python中文字符串前加u
我明明在编码前就加上了# -*- coding: UTF-8 -*-可是运行时还是出错了, # -*- coding: UTF-8 -*- 这句是告诉python程序中的文本是utf-8编码,让pyt ...
- Day13 SQLAlchemy连表操作和堡垒机
一.数据库操作 1.创建表.插入数据和一对多查询 #!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng from sq ...