LeetCode 226. Invert Binary Tree
来源:互联网 发布:floyd算法 路径 编辑:程序博客网 时间:2024/06/08 08:41
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 11.BFS
class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return NULL; queue<TreeNode*> q; q.push(root); while(!q.empty()){ TreeNode* node = q.front(); q.pop(); TreeNode* node2 = node->left; node->left = node->right; node->right = node2; if(node->left) q.push(node->left); if(node->right) q.push(node->right); } return root; }};2.DFS
class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return root; TreeNode* node = root->left; root->left = root->right; root->right = node; if(root->left) invertTree(root->left); if(root->right) invertTree(root->right); return root; }};
0 0
- 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
- 226. Invert Binary Tree LeetCode
- 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
- leetcode: 226. Invert Binary Tree
- 乐乐音乐3.0
- 用继承(inheritance)和组合(composition)设计
- 4.1.2表达式的表示
- Matlab绘图-详细全面(图)
- 判断单链表是否为回文
- LeetCode 226. Invert Binary Tree
- 决策树中的id3算法理解
- EL表达式遍历大小写
- 【usaco 2013 Mar Bronze】种类分配
- VisualSvn Server的使用
- 快速幂+费马小定理 690D2 - The Wall (medium)CF
- HTML5与HTML4的区别
- SQL Server的触发器用法
- **PYTHON** 函数