Invert a binary tree.

Example:

Input:

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

Output:

  1. 4
  2. / \
  3. 7 2
  4. / \ / \
  5. 9 6 3 1
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode invertTree(TreeNode root) {
  12. if (root == null) {
  13. return null;
  14. }
  15. TreeNode left = invertTree(root.left);
  16. TreeNode right = invertTree(root.right);
  17. root.left = right;
  18. root.right = left;
  19. return root;
  20. }
  21. }

[LC] 226. Invert Binary Tree的更多相关文章

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

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

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

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

  4. <LeetCode OJ> 226. Invert Binary Tree

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

  5. Python解Leetcode: 226. Invert Binary Tree

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

  6. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  7. 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): ...

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

  9. (easy)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 was ...

随机推荐

  1. 网页时不时打不开?试试阿里DNS 233.5.5.5 /233.6..6.6

    最经上网都是用手机热点,但发现用谷歌浏览器时,时不时打不开网页.最后发现是DNS的问题,原来我的dns是8.8.8.8. 最后更改成阿里的DNS 233.5.5.5 /233.6..6.6,打开网页流 ...

  2. Xcode7以后访问http加密

    Xcode7 在Info.plist中add Row添加NSAppTransportSecurity类型Dictionary. 在NSAppTransportSecurity下添加NSAllowsAr ...

  3. 201604-1 折点计数 Java

    思路: 这个题要小心考虑不全.左右两边都比这个数小 或者 左右两边都比这个数大 import java.util.Scanner; public class Main { public static ...

  4. c语言中用简单方法对多维数组进行初始化

    例:int array[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12}; 说明:a.由4*3可知,本二维数组包含12个元素,因此初始化时array[0][0] = 1 ,ar ...

  5. 用c语言实现的几个小项目

    1.参考:Linux系统编程 2.参考:制作简单计算器 3.参考:制作2048小游戏 4.参考:五子棋实现

  6. Evaluation metrics for classification

    Accuracy/Error rate ACC = (TP+TN)/(P+N) ERR = (FP+FN)/(P+N) = 1-ACC Confusion matrix Precision/Recal ...

  7. drf三大认证:认证组件-权限组件-权限六表-自定义认证组件的使用

    三大认证工作原理简介 认证.权限.频率 源码分析: from rest_framework.views import APIView 源码分析入口: 内部的三大认证方法封装: 三大组件的原理分析: 权 ...

  8. win10下安装cygwin全过程

    简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...

  9. Unicode的认识

    Unicode(统一码.万国码.单一码),它是为解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制码,以满足跨语言跨平台进行文本转换.处理的要求.1990年开始研 ...

  10. goweb-安装go及配置go

    安装go及配置go 安装go 写这篇博客时,我的电脑的windows已经安装过了go,用的是标准包安装,不过我的linux操作系统还没安装,可以考虑用第三方工具安装,因为看了goweb这本书,我才知道 ...