LeetCode:翻转二叉树【226】

题目描述

翻转一棵二叉树。

示例:

输入:

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

输出:

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

题目分析

  略。

Java题解

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

  

LeetCode:翻转二叉树【226】的更多相关文章

  1. leetcode 翻转二叉树

    翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...

  2. Java实现 LeetCode 226 翻转二叉树

    226. 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max ...

  3. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  4. [LeetCode] 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 ...

  5. 【leetcode 简单】 第六十四题 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...

  6. 领扣(LeetCode)翻转二叉树 个人题解

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题  ...

  7. [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  8. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  9. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

随机推荐

  1. nginx中,$request_uri和$uri的区别

    nginx中,$request_uri和$uri的区别   $request_uri This variable is equal to the *original* request URI as r ...

  2. jquery怎么实现页面刷新后保留鼠标点击addclass的样式

    $(document).ready(function(){ $('#rating li').each(function(){ if($($(this)).attr('id')==String(wind ...

  3. C++ 类的多态五(多态的语法本质分析)

    //多态的语法本质分析 #include<iostream> using namespace std; /* 三种易混淆的多态场景 */ class Point{ public: Poin ...

  4. redis php 实例二

    前面一篇博客主要是string类型,list类型和set类型,下面hash类型和zset类型 1,hset 描述:将哈希表key中的域field的值设为value.如果key不存在,一个新的哈希表被创 ...

  5. (转)TCP三次握手四次挥手

    转自:http://www.jellythink.com/archives/705 参考:http://blog.csdn.net/whuslei/article/details/6667471 [注 ...

  6. SQL Server跨server之间訪问

    在两个server都须要启用Ad Hoc Distributed Queries: EXEC sp_configure 'show advanced options', 1 RECONFIGURE E ...

  7. hdu 2117:Just a Numble(水题,模拟除法运算)

    Just a Numble Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. cygwin下编译zlib源代码

    本文介绍在cygwin下编译zlib源代码的方法步骤,这个过程尽管简单,但还是有一些须要注意的地方. zlib源代码下载地址: http://www.zlib.net/ 下载后.解压就会生成zlib- ...

  9. Python 构造函数、 Python 析构函数、Python 垃圾回收机制

    构造函数与析构函数 构造函数: 用于初始化类的内容部状态,Python提供的构造函数式 __init__(); 也就是当该类被实例化的时候就会执行该函数.那么我们就可以把要先初始化的属性放到这个函数里 ...

  10. Android无线测试之—UiAutomator UiScrollable API介绍三

    获取列表子元素 一.相关API介绍 返回值 API 描述 UiObject getChildByDescription(UiSelector childPattern, String text, bo ...