Leetcode Flatten Binary Tree to Linked List
来源:互联网 发布:三国杀认证裁判知乎 编辑:程序博客网 时间:2024/06/01 14:42
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The 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* now = root; while(now) { if(now->left) { TreeNode* pre = now->left; while(pre->right != NULL) pre = pre->right; pre->right = now->right; now->right = now->left; now->left = NULL; } now = now->right; } }};
阅读全文
0 0
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- [Leetcode]Flatten Binary Tree to Linked List
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- LeetCode:Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- 类和结构体的区别
- 电脑内存
- 结构体定义 typedef struct …
- 动态分配内存(为什么要动态分配内…
- 多线程
- Leetcode Flatten Binary Tree to Linked List
- JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
- C语言的条件编译#if, #elif…
- 指针和引用的区别
- js中跳转页码的方式
- C语言版常见的各种排序算法(…
- Huffman算法简介
- JPEG压缩算法
- 真彩色与伪彩色、直接色的区别