题目:

Invert a binary tree.

4

/ \

2 7

/ \ / \

1 3 6 9

to

4

/ \

7 2

/ \ / \

9 6 3 1

思路分析:

题意是将二叉树全部左右子数对调,如上图所看到的。

详细做法是,先递归处理左右子树,然后将当前的左右子树对调。

代码实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
if(root.left!=null){
root.left=invertTree(root.left);
}
if(root.right!=null){
root.right=invertTree(root.right);
}
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
return root;
}
}

Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]的更多相关文章

  1. LeetCode OJ 226. 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 was ...

  2. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  3. <LeetCode OJ> 226. Invert Binary Tree

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

  4. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  5. 226. Invert Binary Tree(C++)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  6. 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 ...

  7. Python解Leetcode: 226. Invert Binary Tree

    leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...

  8. Leetcode 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...

  9. Java for LeetCode 226 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 ...

随机推荐

  1. 山东省第四届ACM程序设计竞赛A题:Rescue The Princess

    Description Several days ago, a beast caught a beautiful princess and the princess was put in prison ...

  2. Codeforces 980 D. Perfect Groups

    \(>Codeforces\space980 D. Perfect Groups<\) 题目大意 : 设 \(F(S)\) 表示在集合\(S\)中把元素划分成若干组,使得每组内元素两两相乘 ...

  3. 用ExifInterface读取经纬度的时候遇到的一个问题

    如果读取图片经纬度,使用 String latValue = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE); String ln ...

  4. BeanFactoryPostProcessor和BeanPostProcessor

    1. BeanFactoryPostProcessor调用(见AbstractApplicationContext.refresh): >> 创建DefaultListableBeanFa ...

  5. Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题

    C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...

  6. iOS 本地消息推送机制

    转发自:https://www.jianshu.com/p/e347f999ed95     //已经废除的 http://blog.csdn.net/three_zhang/article/deta ...

  7. XY8782S00 BL-W8782 BL-R8782MS1 SDIO接口 高性能、低功耗、体积小 wifi无线模块

    1.产品简介 BL-8782是一款高性能.低功耗.体积小SDIO接口无线模组,符合IEEE802.11N标准,并向下兼容IEEE802.11B/G标准,支持IEEE 802.11i安全协议,以及IEE ...

  8. 企业应用:浅谈 “数据权限” 和 查询 API 设计

    背景 多数企业应用都需要对数据权限进行控制,如:某个用户只能看到某个范围的数据(数据行).某个用户只能看到某几列数据(数据列).本文以数据行级别的权限控制为范例,谈谈如何设计权限模型和查询 API. ...

  9. PulsingHalo(自定义涟漪)的使用方法

    PulsingHalo的git地址是: https://github.com/shu223/PulsingHalo Objective - C 一丶PulsingHalo的实例化和启动: Pulsin ...

  10. Run Redis On Windows

    If you go to the current version and open up the bin > release folder, you'll get a ZIP file cont ...