题目

翻转一棵二叉树

您在真实的面试中是否遇到过这个题?

Yes
样例

  1         1
/ \ / \
2 3 => 3 2
/ \
4 4 和前序遍历代码很相似
从叶子节点依次翻转递归到根节点
C++代码
void invertBinaryTree(TreeNode* root)
{
stack<TreeNode*> s;
TreeNode* p = root;
while (p || !s.empty())
{
while (p)
{
s.push(p);
p = p->left;
}
TreeNode* pa = s.top();
TreeNode* left = pa->left;
TreeNode* right = pa->right;
pa->left = right;
pa->right = left;
p = pa->left;
s.pop();
}
}

  

 

LintCode_175 翻转二叉树的更多相关文章

  1. [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 ...

  2. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  3. [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 ...

  4. leetcode 翻转二叉树

    翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...

  5. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  6. 【leetcode 简单】 第六十四题 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...

  7. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

  8. 领扣(LeetCode)翻转二叉树 个人题解

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题  ...

  9. 翻转二叉树(深搜-先序遍历-交换Node)

    题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...

随机推荐

  1. 前端面试题(js部分)

    一.==和===的区别 1.==   用于比较.判断两者相等,比较时可自动换数据类型 2.===  用于(严格)比较.判断两者(严格)相等,不会进行自动转换,要求进行比较的操作数必须类型一致,不一致时 ...

  2. Spring Cloud各组件

    讲的不错:http://www.ityouknow.com/springcloud/2017/05/16/springcloud-hystrix.html Spring Cloud技术应用从场景上可以 ...

  3. Codeforces 442B. Andrey and Problem

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Leetcode938. Range Sum of BST二叉搜索树的范围和

    给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和. 二叉搜索树保证具有唯一的值. 示例 1: 输入:root = [10,5,15,3,7,null,18], L = 7 ...

  5. PHP--时间搜索插件封装

    /** * 时间搜索插件封装 * anthor qinpeizhou * @param $timeset 时间格式 * @param $time sql语句中所需要搜索的time字段名称 * @par ...

  6. SQL Server数据库存储过程的异常处理

    SQL Server数据库存储过程的异常处理是非常重要的,明确的异常提示能够帮助我们快速地找到问题的根源,节省很多时间.本文我们就以一个插入数据为例来说明SQL Server中的存储过程怎么捕获异常的 ...

  7. Error-SQLServer:【失败】win7装SQL server2017

    ylbtech-Error-SQLServer:[失败]win7装SQL server2017 1.返回顶部 1. 2018年08月15日 22:06:38 USCWIFI 阅读数:5433    版 ...

  8. [C#] double指定有效位数格式化

    C#里面指定小数位数格式化大家都知道 ff.ToString("F3") 可以指定精确到三位小数. 但是如何指定有效位数呢?方法是 ff.ToString("G3&quo ...

  9. SpringMVC配置顺序的问题

    1:web.xml:web应用一经加载,先来找他         1):指明applicationContext的位置         2):引入spring监听,ContextLoaderListe ...

  10. 利用TensorFlow识别手写的数字---基于两层卷积网络

    1 为什么使用卷积神经网络 Softmax回归是一个比较简单的模型,预测的准确率在91%左右,而使用卷积神经网络将预测的准确率提高到99%. 2 卷积网络的流程 3 代码展示 # -*- coding ...