leetcode 226. Invert Binary Tree | DFS 递归转迭代
来源:互联网 发布:餐饮收银软件排名 编辑:程序博客网 时间:2024/05/21 07:48
Description
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
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.
My close solution
class Solution {public: TreeNode *invertTree(TreeNode *root) { if(root==NULL) return NULL; root->left = invertTree(root->right); root->right = invertTree(root->left); return root; }};
上述代码基本思路正确, 但是写法上犯了swap错误写法的低级错误.
Discuss
- C++ 递归
TreeNode* invertTree(TreeNode* root) { if (root) { invertTree(root->left); invertTree(root->right); std::swap(root->left, root->right); } return root;}
- Java 递归
public TreeNode invertTree(TreeNode root) { if (root == null) { return null; } TreeNode right = invertTree(root.right); TreeNode left = invertTree(root.left); root.left = right; root.right = left; return root;}
上述两种写法基本思路是一致的, 尤其是java版本, 跟我的solution只差一个显示的swap正确写法.
- C++ stack dfs
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;}
递归改迭代
二叉树先序遍历递归算法伪码
void PreorderRecursive(Bitree root){ if (root) { /// 先visit visit(root); // 再压入子树 PreorderRecursive(root->lchild); PreorderRecursive(root->rchild); }}
二叉树先序遍历非递归伪码
void PreorderNonRecursive(Bitree root){ stack stk; stk.push(root); while(!stk.empty()){ p = stk.top(); /// 先对p进行处理!!!!! visit(p); stk.pop(); /// 再压入子树 /// 注意这里先压入的right, 这样子才是和上面等价的 if(p.rchild) stk.push(stk.rchild); if(p.lchild) stk.push(stk.lchild); }}
自己修改递归为stack形式, 与参考略有不同
/// 修改递归为stack的迭代形式class Solution {public: TreeNode *invertTree(TreeNode *root) { if(!root) return root; stack<TreeNode *> s; TreeNode *cur; s.push(root); while (!s.empty()) { cur = s.top(); s.pop(); /// 1. 注意这里先处理cur, 即visit(cur)的所有应该做的处理 TreeNode *left = cur->left; TreeNode *right = cur->right; cur->left = right; cur->right = left; /// 2. cur遍历过了, 压入两个子树 if (cur->right) s.push(cur->right); if (cur->left) s.push(cur->left); } return root; }};
Reference
-leetcode 226
-递归算法转换为非递归算法的技巧
阅读全文
0 0
- leetcode 226. Invert Binary Tree | DFS 递归转迭代
- LeetCode oj 226. Invert Binary Tree (DFS||BFS)
- 226.leetcode Invert Binary Tree(easy)[二叉树 递归]
- 【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
- 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
- Go 反射 学习笔记
- 《Close the Loop: Joint Blind Image Restoration and Recognition with Sparse Representation Prior》阅读笔记
- 中国剩余定理的完美演绎--转载于Head For Better
- Android学习笔记——服务最佳实践
- Git 使用基本操作
- leetcode 226. Invert Binary Tree | DFS 递归转迭代
- Emacs文件管理
- 使用Onvif协议进行设备RTSP地址获取
- 电子词典C++实现
- 55.jump游戏
- ACM常用C++模板 包括常用头文件
- iOS 使用UICountingLabel实现数字变化的动画效果
- dispatchDraw
- Nginx Tomcat