题目:

Invert Binary Tree

Total Accepted: 20346 Total Submissions: 57084My Submissions

Question Solution 

Invert a binary tree.

     4
/ \
2 7
/ \ / \
1 3 6 9

to

     4
/ \
7 2
/ \ / \
9 6 3 1

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

代码:

/**
* 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 ) return root;
TreeNode* tmp = root->left;
root->left = Solution::invertTree(root->right);
root->right = Solution::invertTree(tmp);
return root;
}
};

tips:

翻转二叉树跟交换两个int差不多。。。重点是留一个tmp。

【Invert Binary Tree】cpp的更多相关文章

  1. 【Balanced Binary Tree】cpp

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  2. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  3. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  4. 【Lowest Common Ancestor of a Binary Tree】cpp

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  5. 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】

    平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...

  6. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

  7. 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】

    [114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  8. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  9. <LeetCode OJ> 226. Invert Binary Tree

    226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...

随机推荐

  1. CRM, C4C和Hybris的工作流简介

    CRM的例子 Step by Step to debug IC inbox workflow WS14000164 C4C Custom recipient determination in work ...

  2. IOS 分页(pagingEnabled)

    self.scrollView.pagingEnabled = YES; - (void)nextImage { // 1.增加pageControl的页码 ; ) { page = ; } else ...

  3. 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术

    第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...

  4. Spring Security 之集群Session配置

    1.   新建Maven项目 cluster-session 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...

  5. 使用QT开发GoogleMap瓦片显示和下载工具(1)——QT开发环境准备

    由于是第一次使用qt,光是QT的安装和调试就费了好大功夫,汗一个,下面记录下过程和遇到的问题的解决方法吧. 下载QT 直接Google搜索“QT”,进入官网http://qt-project.org/ ...

  6. P1024 一元三次方程求解

    P1024 一元三次方程求解 #include<cstdio> #include<iostream> #include<algorithm> using names ...

  7. inline-block问题

    1.两个相邻的inline-block元素,给inline-block元素设置max-width并且overflow:hidden;之后,相邻行内元素回向下偏移问题 给inline-block元素添加 ...

  8. git 删除本地存在,远程已经删除的分支

    git remote prune origin 强迫症,看到这些分支不一致就来气!

  9. iso十款常用类库

    iso十款常用类库 MBProgressHUD(进展指示符库)   地址:https://github.com/jdg/MBProgressHUD   苹果的应用程序一般都会用一种优雅的,半透明的进度 ...

  10. 关于Pycharm基本操作笔记

    创建 project(工程,译音:破拽科特) 1.Create New project(创建一个新的工程,译音:科瑞特 纽 破摘科特) 2.pure python(纯派森,译音:皮忧儿 派森) 3.l ...