题目

分析

交换二叉树的左右子树。

递归非递归两种方法实现。

AC代码

class Solution {
public:
//递归实现
TreeNode* invertTree(TreeNode* root) {
if (!root)
return root; TreeNode *tmp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(tmp); return root;
} //非递归实现
TreeNode* invertTree2(TreeNode* root) {
if (root == NULL)
return root;
queue<TreeNode*> tree_queue;
tree_queue.push(root); while (!tree_queue.empty()){
TreeNode * pNode = tree_queue.front();
tree_queue.pop(); TreeNode * pLeft = pNode->left;
pNode->left = pNode->right;
pNode->right = pLeft; if (pNode->left)
tree_queue.push(pNode->left);
if (pNode->right)
tree_queue.push(pNode->right);
}
return root;
}
};

LeetCode(226)Invert Binary Tree的更多相关文章

  1. LeetCode(114) Flatten Binary Tree to Linked List

    题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...

  2. LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  3. LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal

    题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...

  4. LeetCode(24)-Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. LeetCode(110) Balanced Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  6. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  7. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

  8. LeetCode(98) Validate Binary Search Tree

    题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...

  9. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

随机推荐

  1. net core WebApi 使用Swagger

    Asp.net core WebApi 使用Swagger生成帮助页 最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core ...

  2. 网络编程之异步IO,rabbitMQ笔记

    对于网络并发编程而言,多线程与多进程算是最常见的需求场景了.毕竟网站开放就是想要更多的流量访问的. 回顾 回顾下之前学过的关于线程,进程和协程的知识点 IO密集型任务--用多线程更好计算密集型任务-- ...

  3. Android商城开发系列(四)——butterknife的使用

    在上一篇博客:Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏实现商城的底部导航栏时,里面用到了butterknife,今天来讲解一下的butterk ...

  4. servlet的重定向和作用域

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  5. HDU 1847 Good Luck in CET-4 Everybody! 四级好运!(博弈)

    思路:先用P/N状态来找规律. N状态:1 2 4 6 8 16 P状态:3 5 因为3=1+2, 无论拿1或者2皆输.看看5,只要抽掉2就变成了3,所以是N状态.看看6,可以抽掉1 2 4,若抽1, ...

  6. rhythmbox插件开发笔记3:安装 makefile && schema && po

    本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...

  7. WCFSVC文件的分离

    项目结构图如下: 新建一个实现内容和接口的项目: 接口内部如下: using DataModel; using System; using System.Collections.Generic; us ...

  8. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

  9. Java 可变长参数列表

    Java中定义了变长参数,允许在调用方法时传入不定长度的参数. 定义及调用 在定义方法时,在最后一个形参后加上三点 …,就表示该形参可以接受多个参数值,多个参数值被当成数组传入.上述定义有几个要点需要 ...

  10. 《大规模 web服务开发》笔记

    大规模服务:     可扩展,负载均衡,保证冗余,低运维成本,开发人数和开发方法的变化 数据处理:     磁盘—>内存—>缓存—>CPU 障碍:     持续增长的服务,”无法在内 ...