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. 快速排序的c++实现 和 python 实现

    最近在学python,其中有个要求实现快速排序的练习,就顺便复习了c++的快速排序实现. 快速排序的基本思想是,通过一轮的排序将序列分割成独立的两部分,其中一部分序列的关键字(这里主要用值来表示)均比 ...

  2. c# 将html添加进剪贴板(带格式)

    调用: ClipboardHelper.CopyToClipboard("<h1>hello world</h1>", ""); /// ...

  3. php获取文件后缀的9种方法

    获取文件后缀的9种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  4. Hadoop2的HA安装(high availability):JournalNode+ zookeeper

    前面介绍过使用NFS+zookeeper来解决namenode单点失败问题,因为NFS可能也会存在单点问题,所以hadoop提供了一种叫做JournalNode技术,这项技术可以在JournalNod ...

  5. 7、手把手教React Native实战之ReactJS

    ReactJS核心思想:组件化  维护自己的状态和UI  自动重新渲染 多个组件组成了一个ReactJS应用 React是全局对象   顶层API与组件API React.createClass创建组 ...

  6. Windows 8.1 浏览器中 SkyDrive 的改名与隐藏

    在 Windows 8.1 中已经整合了 SkyDrive ,在中文版中 SkyDrive 的名字总是感觉不协调,可是在属性里面可以调整位置却不能修改名称,怎么办呢? 打开注册表,找到 HKEY_CL ...

  7. ZBarReaderView屏幕旋转问题

    转载:http://42.96.197.72/ios-zbarreaderview-interface-orientation/ 在iPad应用中,如果没有特殊情况,需要让应用支持所有屏幕方向.在iP ...

  8. 【BZOJ4560】[JLoi2016]字符串覆盖 KMP+状压DP

    [BZOJ4560][JLoi2016]字符串覆盖 Description 字符串A有N个子串B1,B2,…,Bn.如果将这n个子串分别放在恰好一个它在A中出现的位置上(子串之间可以重叠)这样A中的若 ...

  9. struts2的配置文件为什么可以使用${}符号?

    转自:https://www.cnblogs.com/sharpest/p/6030265.html 一.#符号的用途一般有三种. “#”主要有三种用途: 1. 访问OGNL上下文和Action上下文 ...

  10. [LintCode] 正则表达式匹配

    class Solution { public: /** * @param s: A string * @param p: A string includes "." and &q ...