Invert Binary Tree

Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy

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.

 
 
通过这道题,好好理解了递归。
递归返回的是什么要考虑清楚,
可以仔细研磨这道题,很好的题。
 
 
C语言
 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
if (root == NULL)
return root;
if (root->left == NULL && root->right == NULL)
return root;
else{
struct TreeNode* temp = root->right;
root->right = root->left;
root->left = temp;
}
root->left = invertTree(root->left);
root->right = invertTree(root->right); return root;
}

下面是在网上找的一种解法,C++写的。由于这是对实际树的结构进行操作,所以说可以不特别管返回值。

 TreeNode* invertTree(TreeNode* root) {
if (root) {
invertTree(root->left);
invertTree(root->right);
std::swap(root->left, root->right);
}
return root;
}

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

  1. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  2. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  4. 【leetcode】Flatten Binary Tree to Linked List (middle)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  5. 【leetcode】Balanced Binary Tree(middle)

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

  6. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  7. 【LeetCode】107 - Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. 【LeetCode】102 - Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. 【LeetCode】Flatten Binary Tree to Linked List

    随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...

随机推荐

  1. metasploit升级(BT5)

    1.apt-get update 2.apt-get install metasploit 3.修改文件:/opt/metasploit/ruby/lib/ruby/1.9.1/i686-linux/ ...

  2. xfce 双击窗口标题栏无法最大化解决办法

    From: http://hi.baidu.com/jhang20081616/item/0b36ca1d2137df592b3e2219 0.前提是已经在window manager里设置了doub ...

  3. javascript 自调用函数 闭包

    <script type="text/javascript"> var car = {name:"lhs",model:500}; window.o ...

  4. linux+php+apache+mysql(mariadb)故障排除

    wordpress 网页文件打不开(client denied by server).白屏(http 500)问题排除顺序 1.查看apache错误日志查照问题报告找到问题 “client denie ...

  5. git 代码组织

    在20145306CSAPP2E文件夹下建立相应的文件夹: src:存放源代码文件 include: 存放头文件 bin:存放编译后的目标文件.可执行文件等 lib:存放项目所需的静态库.动态(共享) ...

  6. Java中的字符串常量池

    ava中字符串对象创建有两种形式,一种为字面量形式,如String str = "droid";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...

  7. 41. 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的

    lbdeveloplog 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的

  8. imp导入oracle的dmp备份数据

    imp system/oracle fromuser=lc0029999 touser=lc0029999 rows=y commit=y buffer=65536 feedback=10000 ig ...

  9. C# 委托的学习

    delegate int GetCalculatedValueDelegate(int x, int y);    //定义是个委托实际上就是抽象一类  参数列表形式和返回值相同的函数AddCalcu ...

  10. asp.net前台绑定时间格式时,定义时间格式

    <%#Eval("news_time","{0:yyyy-MM-dd}") %><%#((DateTime)Eval("news_t ...