翻转一棵二叉树

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

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. Simple Addition Permits Voltage Control Of DC-DC Converter's Output

    http://electronicdesign.com/power/simple-addition-permits-voltage-control-dc-dc-converters-output In ...

  2. windows系统上安装与使用Android NDK r8d(二)

    四.    在eclipse中集成c/c++开发环境    1. 装Eclipse的C/C++环境插件:CDT,这里选择在线安装.          首先登录http://www.eclipse.or ...

  3. win8升级8.1提示卸载sentinel runtime drivers

    Win8升级8.1时提示需卸载sentinel runtime drivers的解决方法 第一步:打开sentinelcustomer.safenet-inc.com/sentineldownload ...

  4. weblogic打补丁,bsu方法

    刚装了10.3.6版本的weblogic,想把版本补丁到10.3.6.0.12 我用的系统是windows 8.1 ,呵呵 查看版本 执行java weblogic.version WebLogic ...

  5. [翻译] UIView-draggable 可拖拽的UIView

    UIView-draggable 可拖拽的UIView https://github.com/andreamazz/UIView-draggable UIView category that adds ...

  6. go语言基础之不定参数的传递

    1.不定参数的传递 示例1: package main //必须有一个main包 import "fmt" func myfunc(tmp ...int) { for _, dat ...

  7. go语言基础之break和continue的区别

    1.break和continue的区别 在循环里面有两个关键操作break和continue,break操作是跳出当前循环,continue是跳过本次循环. 2.break 备注:break可⽤于fo ...

  8. 今天才知道的JavaScript的真实历史~[转]

    JavaScript真的继承自Cmm吗?JavaScript与Java有多少关系?JavaScirpt最初的设计是怎样的?这个文章是从一个叫编程人生的网站上看到的.不知道出处在哪.在许多资料,Java ...

  9. [AngularJS] Angular1.3 ngAria - 1

    Accessibility is an often overlooked essential feature of a web application. a11y Is a critical comp ...

  10. 彻底领悟javascript中的exec与match方法

    exec是正则表达式的方法,而不是字符串的方法,它的参数才是字符串,如下所示: var re=new RegExp(/\d/); re.exec( "abc4def" ); //或 ...