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 ...
随机推荐
- 爬虫(cookie)——renren模拟登陆
工具:python3 步骤: 1)使用cookiejar.CookieJar()构建一个CookieJar()构建一个对象,用来保存cookie的值 2)使用HTTPCookieProcessor() ...
- ci框架数据库相关函数
返回查询影响的记录数 $res = $this->db->get_where('wx_life',array('id'=>$id)); $num = $res->num_row ...
- WebStorm技巧-集成命令行工具插件
打开菜单项 File -> Settings- 搜索插件 CMD Support,并安装. 重启WebStorm,在你的项目中新建一个Cmd script 文件,命名为build.cmd ...
- 十六进制和ASCII之间的转换
2.关于两个byte[]数组的合并: public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) { byte[] byte_3 = n ...
- 关于window.event.returnValue=false的用处
window.event.returnValue=false放在提交表单中的onclick事件中则不会提交表单,如果放到超链接中则不执行超链接,也就是它禁止了或取消了请求,没有任何效果. 比如: if ...
- css3实现钟表效果
利用css3 transform属性刻画钟表的的刻度以及指针的角度,代码如下: <head> <meta charset="UTF-8"> <titl ...
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载三(通过实例来体验生命周期)
4.1.2 通过实例来亲身体验Activity的生命周期 上一小节介绍了Activity生命周期中的各个过程,本小节将以一个简单的实例来使读者亲身体验到Activity生命周期中的各个事件. 在Ec ...
- [SecureCRT]通过SFTP方式上传本地文件到服务器
1.在本地建一个文件夹,如:d:\My Documents,在此目录下,放入我们需要上传的文件,如:nmon_linux_x86_64 2.然后打开我们的SecureCRT工具,一次选择Options ...
- 新萝卜家园GHOST WIN7系统32,64位官方版下载
来自系统妈:http://www.xitongma.com 新萝卜家园GHOST win7系统64位官方经典版 V2016年3月 系统概述 新萝卜家园ghost win7系统64位官方经典版加快“网上 ...
- lca(最近公共祖先(离线))
转自大佬博客 : https://www.cnblogs.com/JVxie/p/4854719.html LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 首先是最近公共祖先 ...