Invert Binary Tree
来源:互联网 发布:linux解压tar.xz文件 编辑:程序博客网 时间:2024/05/19 22:27
反转二叉树,没仔细看题,层序遍历的思想,写完WA了,一看错误数据,尴尬的反转错了
TreeNode* invertTree(TreeNode* root) { queue<TreeNode*> q; TreeNode* newroot = root; q.push(root); while(!q.empty()){ TreeNode* temp = q.front(); if(temp == NULL){ return NULL; } q.pop(); if(temp->left != NULL && temp->right !=NULL){ int tt = temp->left->val; temp->left->val = temp->right->val; temp->right->val = tt; q.push(temp->left); q.push(temp->right); } } return newroot; }emmm ..重写
TreeNode* invertTree(TreeNode* root) { if(root == NULL){ return NULL; } queue<TreeNode*> q; TreeNode* newroot = root; q.push(root); while(!q.empty()){ TreeNode* temp = q.front(); q.pop(); TreeNode* left = temp->left; temp->left = temp->right; temp->right = left; if(temp->left) q.push(temp->left); if(temp->right) q.push(temp->right); } return newroot; }
阅读全文
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
- CART树回归
- 实验二循环链表
- 位段操作
- 位段的优点
- 数据结构 二叉树 —— 寻找中序遍历的前驱后继
- Invert Binary Tree
- Hive创建分区表
- 实现线程同步的几种方式
- 积分域边界曲线为参数方程的二重积分的计算
- 回归1—pre_train_国庆不回家1
- 步入Hadoop
- Eclipse启动时报错Java was started but returned exit code=13
- HDU 6005 Pandaland 最小环(最小生成树+LCA)
- iOS 委托代理(delegate)入门