226. Invert Binary Tree
来源:互联网 发布:js将字符串转化成数字 编辑:程序博客网 时间:2024/06/04 19:44
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
我的笨方法:DFS
/** * 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 NULL; TreeNode* p_left=invertTree(root->left); TreeNode* p_right=invertTree(root->right); root->left=p_right; root->right=p_left; return root; }};
改进DFS
/** * 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) { invertTree(root->left); invertTree(root->right); std::swap(root->left, root->right); } return root; }};
利用栈或队列的BFS
/** * 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) { std::stack<TreeNode*> stk; stk.push(root); while (!stk.empty()) { TreeNode* p = stk.top(); stk.pop(); if (p) { stk.push(p->left); stk.push(p->right); std::swap(p->left, p->right); } } return root; }};
TreeNode* invertTree(TreeNode* root) { if(nullptr == root) return root; queue<TreeNode*> myQueue; // our queue to do BFS myQueue.push(root); // push very first item - root while(!myQueue.empty()){ // run until there are nodes in the queue TreeNode *node = myQueue.front(); // get element from queue myQueue.pop(); // remove element from queue if(node->left != nullptr){ // add left kid to the queue if it exists myQueue.push(node->left); } if(node->right != nullptr){ // add right kid myQueue.push(node->right); } // invert left and right pointers TreeNode* tmp = node->left; node->left = node->right; node->right = tmp; } return root;}
0 0
- 226.Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 【NOIP2013模拟】四叶草魔杖
- Liunx 学习之旅!
- HDU 1863 又是畅通工程
- 源码、反码和补码易简版
- poj1170shopping office(DP背包问题)
- 226. Invert Binary Tree
- 第十一周项目1-分数类中的运算符重载(1)
- 蓝桥杯第五届决赛B组
- CentOS 7编译安装Nginx1.9.0
- centOs 6.5 ssh配置无密码登入
- 图形的遍历方法之先深后广法
- java中常见面试题总结
- [C++]关于数据永久化的思考(不使用数据库)
- win10 完美屏蔽Flash右键菜单,内存播放Flash swf 文件(VC、 VB)f_in_box4.4 最新破解版