题目

分析

交换二叉树的左右子树。

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

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. 线程池(5)Executors.newScheduledThreadPool

    例子1(scheduleAtFixedRate):延迟2秒后,每隔3秒执行1次 ScheduledExecutorService es = Executors.newScheduledThreadPo ...

  2. 转 AIX filesystemcache引发的Oracle事故

    http://blog.itpub.net/26015009/viewspace-1806629/

  3. 关系型数据库---MySQL---数据表

    1.在创建一个新的MySQL数据表时,可以为它设置一个类型: 2.MySQL支持多种数据表类型,有各自的特点和属性,最重要的3种类型: 1.1 MyISAM 1.2 InnoDB 1.1 可以把Inn ...

  4. 《javascript设计模式》笔记之第九章:组合模式

    之前一直都是按照书的结构顺序做总结,觉得好像不是很好,现在试着完全按照自己的理解做总结.例子还是书上的例子. 一:组合模式的作用: 在web开发中,主要用于创建嵌套的html结点,使得我们方便的把各种 ...

  5. arcgis python 保存当前窗口图形为jpg

    1,第一步打开arcgis 将图形加载进去 第二步,将要保存的图形调到合适的比例尺,然后点击下面按钮 第三步,将写好的python 语句放到里面去: import arcpy mxd = arcpy. ...

  6. Redis特性之持久化机制

    持久化机制 Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化. Redis支持两种持久化方式: 1.snapshotting(快照)也是默认方式 ...

  7. plsql过期注册

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca 打开plsql工具 点击注册即可

  8. go语言简单的soap调用方法

    package main import ( "bytes" "encoding/xml" "fmt" "io" &quo ...

  9. Nginx和Apache服务器上配置反向代理

    在实际项目过程中,由于网站要用到一个在线编辑器(个性化的在线编辑软件),需要跨域进行通信!由于跨域通信较多,所以当时就想到在网站服务器上代理编辑软件的请求! 这就是“反向代理”的实际需求! 一.Ngi ...

  10. VMware与Hyper-V不兼容

    一.问题描述 VMware Workstation与Hyper-V不兼容. 二.解决方案 取消Hyper-V功能,即将Hyper-V框中钩去掉. 三.总结思考 开始不清楚怎么解决这个问题,主要原因在于 ...