交换左右叶子节点

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. void swapLR(TreeNode* root){
  13. if(!root) return;
  14. else{
  15. TreeNode *t = root->left;
  16. root->left = root->right;
  17. root->right = t;
  18. swapLR(root->left);
  19. swapLR(root->right);
  20. }
  21.  
  22. }
  23. TreeNode* invertTree(TreeNode* root) {
  24. swapLR(root);
  25. return root;
  26. }
  27. };

Leetcode 226 Invert Binary Tree 二叉树的更多相关文章

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

  2. LeetCode 226 Invert Binary Tree(转换二叉树)

    翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...

  3. leetcode 226 Invert Binary Tree 翻转二叉树

    大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...

  4. Leetcode 226 Invert Binary Tree python

    题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...

  5. Leetcode 226. Invert Binary Tree(easy)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  6. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  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. ORACLE RMAN备份及还原 RMAN能够进行增量备份:数据库,表空间,数据文件

    ORACLE RMAN备份及还原   RMAN能够进行增量备份:数据库.表空间.数据文件 仅仅有使用过的block能够被备份成backup set 表空间与数据文件相应关系:dba_data_file ...

  2. 使用boost::property_tree生成带attribute的xml

    曾经写过一篇"使用Boost property tree来解析带attribute的xml", 但是还有姐妹篇一直没贴.看看前一篇贴了都快都快3年了,时间过的真快. 这一小篇就算是 ...

  3. Fragment的基本应用

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/50933621 Fragment 还是先来基本介绍. Fragment –> 片段 ...

  4. wikioi 1051哈希表

    题目描写叙述 Description 给出了N个单词,已经按长度排好了序.假设某单词i是某单词j的前缀,i->j算一次接龙(两个同样的单词不能算接龙). 你的任务是:对于输入的单词,找出最长的龙 ...

  5. 关于stm32的RAM大小

  6. js 鼠标坐标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. jquery平滑滚动页面

    滚动到顶部 $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 滚动到指定位置 ...

  8. 【u002】数列排序(seqsort)

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给定一个数列{an},这个数列满足ai≠aj(i≠j),现在要求你把这个数列从小到大排序,每次允许你交 ...

  9. Android Gradle Plugin指南(三)——依赖关系、android库和多项目配置

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Dependencies-Android-Librari ...

  10. POJ1659Frogs&#39; Neighborhood(lavel定理)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7260   Accepted: 31 ...