114. Flatten Binary Tree to Linked List, pre节点的妙用
来源:互联网 发布:java多线程锁 编辑:程序博客网 时间:2024/06/06 05:58
题目:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6代码及思路:
/** * 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 flatten(TreeNode* root) { TreeNode* pre = NULL;//作为右值之前一定要先赋值。 help(root, pre); } void help(TreeNode* &root, TreeNode* &pre){//这里的引用传递,相当于是一个全局变量了 if(root == NULL) return; help(root->right, pre); help(root->left, pre); root->right = pre; root->left = NULL; pre = root; }};
0 0
- 114. Flatten Binary Tree to Linked List, pre节点的妙用
- 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
- 经典加密算法凯撒密码、维吉尼亚密码,RSA算法加密的简单学习
- 最小二乘法直线拟合
- HCTF2016 ATfeild复现和对redis的利用的思考
- jQuery 事件方法
- 开始写一个博客
- 114. Flatten Binary Tree to Linked List, pre节点的妙用
- 抛出异常和捕获异常的区别
- 第十六周项目3--归并排序算法改进
- LA 3695 Distant Galaxy (部分枚举+扫描法)
- android 之popupWindow 在指定位置上的显示
- Metron学习4_Ansible Based Vagrant Single Node VM Install
- confstr--Linux下获取C标准库信息的函数
- pip更换国内源,解决服务器网速贼慢的问题
- 只是上传一个图标