题目:

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. Docker build 安装报错, Could not open requirments file: [Errno 2] No such file or directory:'requirements.txt'

    docker安装教程 https://docs.docker.com/get-started/part2/#build-the-app 相关帖子 https://stackoverflow.com/q ...

  2. 使用nodejs创建加入用户验证的websocket服务

    使用nodejs创建websocket服务是非常简单的(”ws”模块等),网上教程也很多.websocket服务默认没有连接验证,再加上它支持跨域连接,这样就存在“盗连”和并发攻击的风险. nodej ...

  3. POJ-3614 Sunscreen---贪心+优先队列

    题目链接: https://vjudge.net/problem/POJ-3614 题目大意: 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小 ...

  4. python 将表格多个列数据放到同一个单元格中

      表格模板: 目的将卡片1到卡片5的所有数据组合起来到一个单元格中如下入F列中(工作中为了避免手动复制粘贴),其余不变,因为数据太多 自己一个一个复制工作效率太低,所以写这个脚本是为了方便自己有需要 ...

  5. Pj Immediate Decodability

    判断一个串是否是其他的前缀 我们需要建立一颗tire树 在插入边的时候,如果遇到一个其他串的结尾,那么就说明至少有一个串,是插入串的前缀.如果在插入完后没有新增的节点,那么插入的串就是其他串的前缀 # ...

  6. mayan 游戏真是毒瘤

    如题 真坑呀!!! 可算过了 我率先达到了氧气富有化 先是改变时没有fall.40分 然后是fall函数写慢了 tle 50分 . 上代码 #include<cstdio> #includ ...

  7. cudaMallocPitch – 向GPU分配存储器

    概要 cudaError_t cudaMallocPitch( void** devPtr,size_t* pitch,size_t widthInBytes,size_t height ) 说明 向 ...

  8. 第34章 IWDG—独立看门狗—零死角玩转STM32-F429系列

    第34章     IWDG—独立看门狗 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  9. redis 过期回调通知

    redis 过期回调通知 背景 在使用redis的过程中,有时我们会遇到这种情景,当key过期的时候,我们需要去重新做一些操作,比如重新生成value等.之前,一直用的是添加一个celery定时任务, ...

  10. GUI测试问题汇总

    1.ajax实现的页面元素定位问题 最近在做项目的时候遇到一个问题,通过xpath定位到元素后做一个循环操作,第一循环可以正常执行,第二次循环后就报错,错误信息:Message: The elemen ...