题目

分析

交换二叉树的左右子树。

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

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. Netty-flush

    TimerServer: ch.pipeline().addLast(new TimeEncoder()); ch.pipeline().addLast(new TimeServerHandler() ...

  2. [FACT_采购信息]增加了延期天数

    [延期天数]是指的采购单上的货品交货日期 减 [厂家来货]单据货品第一次到货日期. [FACT_采购信息] SELECT p.[Purchase_ID] [采购单号ID], p.[Supply_No] ...

  3. java中接口(interface)和虚基类(abstract class)的区别

    在Java语言中,abstract class和interface是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的面向对象能力.abstract class和interfa ...

  4. python的subprocess模块(写的不错留作查询)

    python的subprocess模块 subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*. ...

  5. SQL Server 2012安装配置(Part1 )

    1 安装前准备 安装 SQL Server 2012 服务器及客户端前,需要提前做以下两项准备: SQL Server2012 依赖于.Net Framework 3.5.1 组件.Windows S ...

  6. robotframework介绍

    1.测试用例使用文本文件(TXT或者TSV文件)保存,使用制表符分隔数据.可以方便的使用任何文本编辑器,或者EXCEL编辑测试用例.也可以使用HTML格式创建用例.2.测试用例中支持变量使用,可以使用 ...

  7. 参考消息 Android 读报

    <参考消息>是新华通讯社主办,参考消息报社编辑出版的日报,创刊于1931年,历史长达80年.<参考消息>每天及时选载世界各国(地区)通讯社.报刊及因特网上的最新消息.评论的精华 ...

  8. HDU 3652 B-number (数位DP,入门)

    题意: 如果一个整数能被13整除,且其含有子串13的,称为"B数",问[1,n]中有多少个B数? 思路: 这题不要用那个DFS的模板估计很快秒了. 状态设计为dp[位数][前缀][ ...

  9. C基础的练习集及测试答案(1-15)

    练习题:注:标有(课堂)字样的为课上练习,其他为课下练习基础题(50题)1.(课堂)编写程序,输出“XXX欢迎来到动物园!”(XXX是自己的名字). //1.(课堂)编写程序,输出“XXX欢迎来到动物 ...

  10. java面试题(杨晓峰)---第三讲谈谈final、finally、finalize有什么不同?

    java语言有很多看起来相似,但用途却完全不相同的语言要素,这些内容往往容易成为面试官考察你知识掌握程度的切入点. 今天我要问你一个基础的java经典题目,谈谈final.finally.finali ...