[LeetCode 226] Invert Binary Tree
来源:互联网 发布:无线传屏软件 编辑:程序博客网 时间:2024/06/05 02:02
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1
递归的方法:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* invertTree(TreeNode* root) {if (root == NULL)return root;TreeNode *tmp = root->left;root->left = invertTree(root->right);root->right = invertTree(root->left);return root;}};
非递归的方法:
lass Solution {public:TreeNode* invertTree(TreeNode* root) {queue<TreeNode*> tree_ueue;if(root == NULL)return root;tree_queue.push(root);while(!ree_queue.empty()){TreeNode* pNode = tree_queue.front();tree_queue.pop();TreeNode* pTmp = pNode->left;pNode->left = pNode->right;pNode->right = pTmp;if(pNode->left)tree_queue.push(pNode->left);if(pNode ->right)tree_queue.push(pNode->right);}return root;}};
0 0
- Leetcode[226]-Invert Binary Tree
- LeetCode 226 Invert Binary Tree
- Invert Binary Tree - LeetCode 226
- leetcode[226]:Invert Binary Tree
- leetcode 226: Invert Binary Tree
- Leetcode 226 Invert Binary Tree
- leetcode-226-Invert Binary Tree
- leetcode 226 :Invert Binary Tree
- LeetCode 226Invert Binary Tree
- leetCode #226 Invert Binary Tree
- [LeetCode 226] Invert Binary Tree
- Leetcode #226 Invert Binary Tree
- leetcode 226: Invert Binary Tree
- leetcode: (226) Invert Binary Tree
- LeetCode 226----Invert Binary Tree
- LeetCode 226: Invert Binary Tree
- leetcode 226--Invert Binary Tree
- 【LEETCODE】226-Invert Binary Tree
- 统计一段英文中单词出现的次数
- 微机原理==第二章16位和32位微处理器(4)
- 博客 first
- select系统调用与FD_SET,FD_ISSET,FD_ZERO
- 查看进程的内存和CPU占用
- [LeetCode 226] Invert Binary Tree
- Android Hongyang博客自定义View练习与优化
- Maven依赖机制 工作原理 (分析maven项目和非maven项目的差别)
- Win7安装Visual Studio2015 失败的解决方案
- js实现checkbox全选功能
- 直接用电脑下载google play应用
- 判断栈的出栈顺序是否正确
- platform 平台总线
- 求空间两条直线之间的距离