翻转二叉树-LintCode
来源:互联网 发布:淘宝店铺支付宝限额 编辑:程序博客网 时间:2024/06/05 20:53
翻转一棵二叉树
样例:
递归&非递归:
#ifndef C175_H#define C175_H#include<iostream>#include<stack>using namespace std;class TreeNode{public: int val; TreeNode *left, *right; TreeNode(int val) { this->val = val; this->left = this->right = NULL; }};class Solution {public: /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { // write your code here if (root == NULL) return; if (root->left != NULL || root->right != NULL) { TreeNode *node = root->left; root->left = root->right; root->right = node; } invertBinaryTree(root->left); invertBinaryTree(root->right); }};class Solution2 {public: /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { // write your code here if (root == NULL) return; stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode *p = s.top(); s.pop(); TreeNode *node = p->left; p->left = p->right; p->right = node; if (p->left != NULL) s.push(p->left); if (p->right != NULL) s.push(p->right); } }};#endif
阅读全文
0 0
- LintCode 翻转二叉树
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- LintCode-翻转二叉树
- LintCode 翻转二叉树
- 翻转二叉树-LintCode
- lintcode 翻转二叉树
- LintCode 翻转二叉树
- 翻转二叉树-LintCode
- lintcode翻转二叉树
- LintCode - 175.翻转二叉树
- lintcode 175 翻转二叉树
- [LintCode]175.翻转二叉树
- [Lintcode] #175 翻转二叉树
- 二叉树专题-lintcode翻转二叉树
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode--翻转二叉树(非递归)
- lintcode——翻转二叉树
- javase-关键字、标识符
- Android 百度地图开发(三)--- 实现比例尺功能和替换自带的缩放组件
- 浏览器出现代理服务器拒绝连接的解决办法
- csrf攻击过程 csrf攻击说明 1.用户C打开浏览器,访问受信任网站A,输入用户名和密码请求登录网站A; 2.在用户信息通过验证后,网站A产生Cookie信息并返回给浏览器,此时用户登录网站
- 一步一步学多线程-synchronized
- 翻转二叉树-LintCode
- 并发编程6:CopyOnWriteArrayList 的写时复制
- cnt
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
- POI中文API文档
- Codeforces Round #429 (Div. 2) A-C题解
- Javascript—Break和Continue语句
- 语法:STL中的有关二分查找的算法