Invert Binary Tree
来源:互联网 发布:数控车u型槽的编程 编辑:程序博客网 时间:2024/04/28 10:23
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) return root; TreeNode* tmp = root->left; root->left = invertTree(root->right); root->right = invertTree(tmp); return root; }};
Solution2,类似树的层次遍历,使用队列
/** * 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) return root; queue<TreeNode *> myQueue; myQueue.push(root); while(!myQueue.empty()) { TreeNode * curNode = myQueue.front(); myQueue.pop(); TreeNode * tmpNode = curNode->left; curNode->left = curNode->right; curNode->right = tmpNode; if(curNode->left) myQueue.push(curNode->left) ; if(curNode->right) myQueue.push(curNode->right) ; } return root; }};
0 0
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- Invert Binary Tree
- leetcode:Invert Binary Tree
- Invert Binary Tree
- LeetCode Invert Binary Tree
- 226Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- Invert Binary Tree
- leetcode : Invert Binary Tree
- 226Invert Binary Tree
- Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- Python使用__future__
- 回溯算法
- 用Python和Pygame写游戏-从入门到精通(14)
- 二叉树系统
- poj 1416 Shredding Company 二进制枚举
- Invert Binary Tree
- JVM -XX:NewRatio、-XX:SurvivorRatio参数含义
- 用Python和Pygame写游戏-从入门到精通(15)
- Tomcat启动时卡在“INFO: Deploying web application directory ......”的解决方法
- 用Python和Pygame写游戏-从入门到精通(16)
- BNUOJ 12887 isumi (计算几何+最小割)
- jquery 的datatable的属性说明
- 用Python和Pygame写游戏-从入门到精通(17)
- Windows编程微秒级延时的实现