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. CI框架中 日志输出方法log_message()只允许输出字符串解决方案

    1.修改CodeIgniter/index.php,添加如下: define('ROOTDIR',dirname(__FILE__).'/'); 2.修改CodeIgniter/application ...

  2. 什么是 end-to-end 神经网络?——知乎解答

    什么是 end-to-end 神经网络? https://www.zhihu.com/question/51435499 解答1 张旭 像机器一样学习,像人一样生活 YJango 等   端到端指的是 ...

  3. 快速开发微信小程序

    image.png 最近婷主在做微信小程序.自己的微信公众号也需要添加点料,乘着这次放假,把微信小程序研究了下.虽然没有做什么很强大的功能,不过好歹自己的公众号也有了微信小程序.够用即可. 1.需要先 ...

  4. php视频教程

    网址:http://www.php100.com/index.html

  5. jQuery特效:图片的轮播

    Flexslider图片轮播.文字图片相结合滑动切换效果 地址:http://www.helloweba.com/view-blog-265.html 示例:http://www.helloweba. ...

  6. http://blog.csdn.net/v_july_v/article/details/6543438

    本文转载至: http://blog.csdn.net/v_july_v/article/details/6543438 算法 程序员面试.算法研究.编程艺术.红黑树.数据挖掘5大经典原创系列集锦与总 ...

  7. iOS中的armv6、armv7、armv7s

    armv6.armv7.armv7s是arm CPU的指令集,原则上是向下兼容的,如:iPhone4sCPU支持armv7,但它会兼容armv6,只是使用armv6指令可能无法充分发挥它的特性.iph ...

  8. 关于TNS_ADMIN环境变量

    转自:https://blog.csdn.net/pan_tian/article/details/7699599 很多oracle产品都有自己的TNS文件,如果你的系统里装了多个Oracle的产品的 ...

  9. 原生JS返回顶部,带返回效果

    有些网站当滑到一定高度时右下角会有一个按钮,你只要一点就可以直接返回顶部了.那这个功能是怎么做到的呢.其实不算太难: 首先我们先在网页中创建一个按钮,上面写上返回顶部,把它的样式改成固定定位,之后想要 ...

  10. python之MySQL学习——防止SQL注入(参数化处理)

    import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标对象 c ...