LeetCode226 翻转二叉树
翻转一棵二叉树。
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
备注:
这个问题是受到 Max Howell的 原问题 启发的 :
谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
/**
* 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* invertTree(TreeNode* root) {
if (root == NULL)
return NULL
TreeNode *tmp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(tmp);
return root;
}
};
/*
算法思想:
非递归的方法也不复杂,跟二叉树的层序遍历一样,需要用queue来辅助,先把根节点加入队列中,然后从队中取出来,交换其左右节点,如果存在则分别将左右节点在加入队列中,以此类推直到队列中没有节点了停止循环,返回root即可。
*/
//算法实现: class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (!root)
return NULL;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode *node = q.front(); q.pop(); TreeNode *tmp = node->left;
node->left = node->right;
node->right = tmp; if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
return root;
}
};
LeetCode226 翻转二叉树的更多相关文章
- [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- leetcode 翻转二叉树
翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【leetcode 简单】 第六十四题 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- 领扣(LeetCode)翻转二叉树 个人题解
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 ...
- 翻转二叉树(深搜-先序遍历-交换Node)
题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...
随机推荐
- 题解 CF1062E Company
\(\texttt{Solution}\) 数据结构学傻的蒟蒻来写一个新思路 这题的正解是利用多个结点的 \(lca\) 是 \(dfs\) 序最大的结点和 \(dfs\) 序最小的结点的 \(lca ...
- 学习笔记:Prufer 编码
Prufer 编码可以将无根树与序列之间进行转化. 一个 \(n\) 个点.区分编号的无向图 和 Prufer 序列一定是一一对应的,下面会给出映射方式. 借此可以证明 Cayley 定理: \(n\ ...
- MySQL和sparkSQL合并行
表A 表B 从表A到表B MySQL 写法:select name, group_concat(score seperate ';') as score from A group by name sp ...
- Python 学习笔记 之 03 - 函数总结
函数总结 最基本的一种代码抽象的方式. 定义函数 使用def语句进行定义, return进行函数返回. 一旦执行导return,函数就执行完毕. 即使函数未指定retur ...
- 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于Jsoup)
1. 跨站点脚本编制 风险:可能会窃取或操纵客户会话和 cookie,它们可能用于模仿合法用户,从而使黑客能够以该用户身份查看或变更用户记录以及执行事务. 原因:未对用户输入正确执行危险字符清 ...
- 2020-2021-1 20209307《Linux内核原理与分析》第四周作业
一.Linux内核源代码简介 1.计算机三大法宝 存储程序计算机 函数调用堆栈 中断机制 2.操作系统两把宝剑 中断上下文的切换 进程上下文的切换 3.函数目录 Linux-3.18.6/arch/x ...
- 钩子与API截获
http://www.pudn.com/Download/type/id/19.html
- 在库中使用schematics——ng add与ng update
起步 创建一个angular库 ng new demo --create-application=false ng g library my-lib 可见如下目录结构 ├── node_modules ...
- BERT 论文阅读笔记
BERT 论文阅读 BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 由 @快刀切草莓君 ...
- 小白都能理解的Python多继承
本文主要做科普用,在真实编程中不建议使用多重继承,或者少用多重继承,避免使代码难以理解. 方法解析顺序(MRO) 关于多重继承,比较重要的是它的方法解析顺序(可以理解为类的搜索顺序),即MRO.这个跟 ...