[Leetcode][Tree][Flatten Binary Tree to Linked List ]

按照前序遍历的顺序把树用right连起来。

本来想了半天,一点思路都没有,总觉得Inplace的解法一般都非常巧妙。

后来我突发灵感,决定用一个变量记录当前访问到哪个点,真是太机智了~~

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void flatten(TreeNode *root) {
13         now = NULL;
14         findRes(root);
15     }
16 private:
17     TreeNode * now;
18     void findRes(TreeNode *root) {
19         if (root == NULL) {
20             return;
21         }
22         findRes(root->right);
23         findRes(root->left);
24         root->left = NULL;
25         root->right = now;
26         now = root;
27     }
28 };

一次AC之后看了题解,发现大家的思路差别好大!其中迭代的那个版本感觉很清晰。又写了一遍,同样是一次AC。

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void flatten(TreeNode *root) {
13         if (root == NULL) {
14             return;
15         }
16         stack<TreeNode*> s;
17         s.push(root);
18         while (!s.empty()) {
19             TreeNode * now = s.top();
20             s.pop();
21             if (now->right != NULL) {
22                 s.push(now->right);
23             }
24             if (now->left != NULL) {
25                 s.push(now->left);
26             }
27             now->left = NULL;
28             if (!s.empty()) {
29                 now->right = s.top();
30             }
31         }
32     }
33 };

[Leetcode][Tree][Flatten Binary Tree to Linked List ]

时间: 2024-10-06 16:00:51

[Leetcode][Tree][Flatten Binary Tree to Linked List ]的相关文章

[LeetCode 题解]: Flatten Binary Tree to Linked List

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 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

leetcode 114. Flatten Binary Tree to Linked List (Python版)

题目: Given a binary tree, flatten it to a linked list in-place. 算法思路: 其实该题目就是二叉树前序遍历的变形 我代码沿用leetcode 144. Binary Tree Preorder Traversal 代码: class Solution(object):     def preorderTraversal(self, root):         """         :type root: Tree

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

leetCode(42):Flatten Binary Tree to Linked List

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 If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-o

Java for LeetCode 114 Flatten Binary Tree to Linked List

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解题思路:试图通过排序后new TreeNode是无法通过的,这道题的意思是把现有的树进行剪枝操作.JAVA实现如下: static public void flatten(TreeNode root) { wh

LeetCode (15) Flatten Binary Tree to Linked List

题目描述 Given a binary tree, flatten it to a linked list in-place. For example, Given The flattened tree should look like: 本题也是考察二叉树和指针操作的题目.题目要求将一棵二叉树拉平为一个链表 .链表通过树节点的右子树相连,且展开的顺序为原来树的前序遍历. 实现思路: 若节点n存在左子树left,则将左子树连接为n的右子树 若n存在右子树(n->right存在),则将n->ri

leetcode dfs Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions 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 click to

[C++]LeetCode: 102 Flatten Binary Tree to Linked List (二叉树转前序链表)

题目: 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 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right child po

leetcode 之 Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List 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 思路:对二叉树进行先序遍历,把左子树为空的节点的左子树指向它的后序节点,然后把所有的右子树指向左子树 struct TreeNode { i