【LeetCode二叉树#03】翻转二叉树的几种方法
翻转二叉树
翻转一棵二叉树。
这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈)
思路
使用一种二叉树的遍历方法遍历,然后翻转每个节点的子节点即可
代码
前序遍历(迭代)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
//先定义一个栈
stack<TreeNode*> st;
//检测传入节点是否为空
if(root == NULL) return 0;
//将根节点压栈
st.push(root);//中
//当栈中不为空时持续遍历
while(!st.empty()){
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
//判断当前节点有无左右子节点,有就按顺序压栈(明确你的出栈顺序,入栈时要相反)
if(node->right)st.push(node->right); //右
if(node->left)st.push(node->left); //左
}
return root;
}
};
前序遍历(递归)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
层序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
while(size--){
TreeNode* node = que.front();
que.pop();
swap(node->left, node->right);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return root;
}
};
【LeetCode二叉树#03】翻转二叉树的几种方法的更多相关文章
- 力扣(LeetCode)226. 翻转二叉树
翻转一棵二叉树. 示例: 思想 递归 java版 /** * Definition for a binary tree node. * public class TreeNode { * int va ...
- Leetcode题目226.翻转二叉树(简单)
题目描述: 翻转一颗二叉树 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 1)递归,不断交换左右子树,直到 ...
- 【LeetCode】226. 翻转二叉树
题目 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 本题同[剑指Offer]面试题27. 二叉树的镜 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- selenium 切换窗口的几种方法
第一种方法: 使用场景: 打开多个窗口,需要定位到新打开的窗口 使用方法: # 获取打开的多个窗口句柄 windows = driver.window_handles # 切换到当前最新打开的窗口 d ...
- [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 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 代码随想录算法训练营day14 | leetcode 层序遍历 226.翻转二叉树 101.对称二叉树 2
层序遍历 /** * 二叉树的层序遍历 */ class QueueTraverse { /** * 存放一层一层的数据 */ public List<List<Integer>&g ...
- leetcode 翻转二叉树
翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...
随机推荐
- 【转帖】isolcpus功能与使用
isolcpus功能存在已久,笔者追溯v2.6.11(2005年)那时内核就已经存在了isolcpus功能.根据kernel-parameters.txt 上的解释,"isolcpus功能用 ...
- [转帖]ls命令
ls(list) 命令可以说是Linux下最常用的命令之一 #ls -l;列出文件的详细信息 #ll 以上两个命令一样,ll是ls -l的简写 #ls -al;列出目录下的所有文件,包括以 . 开头的 ...
- CentOS确认网口是否插入网线的办法
最近公司的机器存在网络问题, 部分网络总是不通, 比较奇怪. 最近一直想处理好. 第一步: 先查看网口的设备信息 可以使用 ip link show 可以讲网口信息都展示出来. 一般情况下 NO-C ...
- [译]深入了解现代web浏览器(一)
本文是根据Mariko Kosaka在谷歌开发者网站上的系列文章https://developer.chrome.com/blog/inside-browser-part1/ 翻译而来,共有四篇,该篇 ...
- pytest-xdist分布式
使用pytest框架运行每条case的时候,都是上一条运行结束才会运行下一条,要是有成千上百条case 且每条运行2s那就是2*总条数,会浪费大量的时间和人力.为了节约时间和人力成本,pytest提供 ...
- Spring Boot 统一RESTful接口响应和统一异常处理
一.简介 基于Spring Boot 框架开发的应用程序,大部分都是以提供RESTful接口为主要的目的.前端或者移动端开发人员通过调用后端提供的RESTful接口完成数据的交换. 统一的RESTfu ...
- vue基础系列文章12---创建脚手架
一.交互式命令行创建 1.运行 vue create myvue 选择默认创建模式,会在指定的文件夹下创建文件 2.进入到myvue文件夹,运行:npm run serve 3.访问本地的地址就可以 ...
- gRPC如何保障数据安全传输
什么是 gRPC? gRPC 是由 Google 开发的高性能.开源的 RPC(Remote Procedure Call)框架,用于在客户端和服务器之间进行通信.它基于 Protocol Buffe ...
- SqlSugar的Select用法
Select 位置 正常情况:应该在最后面, 一般是 .Where(..).OrderBy(..).Select(..).ToList() 特殊情况:如果Select不是最后一个位置,则Select要 ...
- MySQL【四】---案例实战{拆分多表、外键创建等}
1.准备数据 数据准备 create database jing_dong charset = utf8mb4; 创建一个商品goods数据表: create table goods( id int ...