114. Flatten Binary Tree to Linked List
来源:互联网 发布:单片机 fc音效 编辑:程序博客网 时间:2024/04/29 01:13
一次AC,前序遍历再重构
/** * 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: void preOrder(TreeNode* root,queue<TreeNode*>& nodes) { if(root==NULL) return; else { nodes.push(root); if(root->left!=NULL) preOrder(root->left,nodes); if(root->right!=NULL) preOrder(root->right,nodes); return; } } void flatten(TreeNode* root) { if(root==NULL) return; queue<TreeNode*> nodes; preOrder(root,nodes); TreeNode* preRoot=nodes.front(); nodes.pop(); while(!nodes.empty()) { preRoot->left=NULL; preRoot->right=nodes.front(); preRoot=nodes.front(); nodes.pop(); } preRoot->left=NULL; preRoot->right=NULL; return; }};
0 0
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- XiaomiRouter自学之路(08-U-boot启动数值具体说明)
- 用IDEA写代码究竟有多爽
- C++中的new delete和new[] delete []
- (1)FPQuant JHipster_总体配置
- spring security之httpSecurity使用示例
- 114. Flatten Binary Tree to Linked List
- 根据URL下载文件到本地(将二进制文件保存到磁盘)
- 如何在Linux系统上监测系统温度?
- MFC Unicode文件读取 分割字符串
- Android热更新开源项目Tinker集成实践总结
- c# String字符串相关
- Android Studio 下 SVN 与 Git+OSChina 使用总结
- 【CQOI2007】矩形(BSOI2021)
- 接口篇(5.4) 03. 交换机连接(单臂路由) ❀ 飞塔 (Fortinet) 防火墙