翻转一棵二叉树

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

Yes
样例

  1         1
/ \ / \
2 3 => 3 2
/ \
4 4 思路:依旧采用递归的思路,判断特殊条件后,先交换根节点的左右孩子,然后再对其左右子树进行递归调用。 代码实现也很简单,一次AC。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
/*
思路:还是采用递归的方法,交换根节点的左右孩子,然后再对左右子树进行递归调用。
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if(root==NULL){
return;
} if(root->left==NULL&&root->right==NULL){
return;
} TreeNode* temp=root->right;
root->right=root->left;
root->left=temp; if(root->right!=NULL){
invertBinaryTree(root->right);
}
if(root->left!=NULL){
invertBinaryTree(root->left);
}
}
};

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

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

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

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

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

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

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

  5. leetcode 翻转二叉树

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

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

  10. 算法 翻转二叉树 dfs

    翻转二叉树 翻转一棵二叉树.左右子树交换. Example 样例 1: 输入: {1,3,#} 输出: {1,#,3} 解释: 1 1 / => \ 3 3 样例 2: 输入: {1,2,3,# ...

随机推荐

  1. cocos2d-x 环境搭建

        刚搬到博客园,第一次在这写博.有点小激动啊~~     闲话不多说,这次想做一个专题,针对最近比较流行的手游开发引擎cocos2d-x,希望大家不吝赐教~     本节主要针对环境搭建方面进行 ...

  2. asp.net Core 中间件Hello world

    虽然在ASP.NET 5中,微软没有再强调OWIN(Open Web Interface for .NET)及其微软官方的OWIN实现Katana,但是其中涉及到一些原则和设计思想依然被ASP.NET ...

  3. DCI:DCI学习总结

    备注 之前把DCI的Role和四色原型的Role给弄混了,本文也不会比较这两种Role的区别(后面有机会再说),这里简单的记录一下对DCI的理解. 参考文章:http://www.cnblogs.co ...

  4. Shell编程入门(第二版)(中)

    注:test測试语句一般不单独使用,一般作为if语句的測试条件,如; if test -d file then .... fi test的变量的简写形式"[]" 演示样例-apac ...

  5. acd The Game about KILL(和约瑟夫归则一样,归律)

    Problem Description Teacher HU and his 40 students were trapped by the brigands. To show their power ...

  6. ffmpeg中的sws_scale算法性能对比

    sws_scale的算法有如下这些选择. #define SWS_FAST_BILINEAR 1#define SWS_BILINEAR 2#define SWS_BICUBIC 4#define S ...

  7. js 操作select和option常用代码整理

    1.获取选中select的value和text,html代码如下: <select id="mySelect"> <option value="1&qu ...

  8. Informatica 常用组件Source Qualifier之二 默认查询

    2 默认查询 对于关系源,PowerCenter Server 将在运行会话时为每个源限定符转换生成查询.对于每个在映射中使用的源列,默认查询均为 SELECT 语句.也就是说,PowerCenter ...

  9. Unable to lock the administration directory (/var/lib/dpkg/),is another process using it?

    Description:无法获得锁 /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)Unable to lock the ...

  10. <The Art of Readable Code> 笔记一

    第1章  代码应易理解 (Code should be easy to understand) 基本原则:好的代码,能够减少 “别人” 理解它的时间. “别人” 不仅指的是 “其它人”,也可能是 “以 ...