LeetCode(226)Invert Binary Tree
题目
分析
交换二叉树的左右子树。
递归非递归两种方法实现。
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的更多相关文章
- LeetCode(114) Flatten Binary Tree to Linked List
题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...
- 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 ...
- 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 ...
- LeetCode(24)-Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode(110) Balanced Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- leetcode笔记(二)94. Binary Tree Inorder Traversal
题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- 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 ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
随机推荐
- 转 db_file_multiblock_read_count
http://www.laoxiong.net/table_scan_and_buffer_cache.html 全表扫描与buffer cache https://www.cnblogs.com/R ...
- X Samara Regional Intercollegiate Programming Contest DIV2
http://codeforces.com/gym/101341 其实我觉得这份题很不错的,虽然是div2,但是感觉对我挺有帮助(我比较垃圾0.0),还没补完(做的时候一直蒙逼,要补很多题)先写一点点 ...
- setState异步函数
changeLeader(value){ console.log(value) this.setState({ leader:value },() => { console.lo ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- 《深入理解java虚拟机》笔记(7)JVM调优(分代垃圾收集器)
以下配置主要针对分代垃圾回收算法而言. 一.堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用 ...
- Spark Mllib里如何将数据取消暂存在内存中(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集
- 机器学习框架ML.NET学习笔记【6】TensorFlow图片分类
一.概述 通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下: 之前介绍 ...
- 复习KMP
KMP刚学的时候,看不懂. 再看,哇!原来是这样! 用的时候,忘了. 为了不再跌倒,我决定,记住吧... 在我看来,KMP一般用于字符串匹配时的防超时优化. 他的精髓就是,利用已经匹配的信息,简化这之 ...
- Sql server 查询指定时间区间工作日数、休息日数等日期操作
1.查询指定时间区间的工作日 这个主要难点是法定节假日,国家的法定节假日每年都不一样,还涉及到调休,所以我们设计一个假日表.主要字段有年份,类型(是否调休),假期日期.如下: CREATE TABLE ...
- 31全志r58平台Android4.4.2下打开USB摄像头
31全志r58平台Android4.4.2下打开USB摄像头 2018/10/26 16:00 版本:V1.0 开发板:SC5806 1.系统编译:(略) 2.需要修改的文件: W:\r58_andr ...