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. 创建struct类型的数组

    在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10] ...

  2. 2016-07-15: Window定时器使用

    windows下定时器使用实例 #include <iostream> #include <Windows.h> using namespace std; void Timer ...

  3. Flask First Look

    from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "He ...

  4. Changing SharePoint Default port ( 80 ) to another port ( 79 ).

      Introduction In this How-To I will change my port from 80 to 79, probably because I want to host s ...

  5. 初尝 JFinal 项目(一)

    temp1: JFinal项目与JAVA项目类似,有属性方法.操作方法.Sql语句操作.jdbc.配置文件 对比:|| JAVA: Bean / Srv(Server) / SqlMap / jdbc ...

  6. MongoDB-JAVA-Driver 3.2版本常用代码全整理(1) - 增删改

    MongoDB的3.x版本java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别.例如用Document替换BasicDBObject.通过Builders类构建Bson替代直接输入$命令等 ...

  7. 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别

    protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...

  8. SQL Server数据库表重置自增主键号(通常是指ID)

    执行 DBCC CHECKIDENT ('table_name', NORESEED) 以确定列中的当前最大值 然后使用 DBCC CHECKIDENT ('table_name', RESEED,n ...

  9. 根据关键词kill进程

    #!/bin/sh pid=`ps -ef | grep /usr/bin/memcached | grep -v grep | awk '{print $2}'` kill $pid

  10. 修改Tomcat根目录

    在server.xml文件中找到</Host>标签,在之前加入这样一行:<Context path="" docBase="F:/MyWeb" ...