[Leetcode 81] 114 Flatten Binary Tree to Linked List
2013-07-23 14:19
555 查看
Problem:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
Analysis:
Solution to tree problem is always recursive. So is this problem. First let's see some basic cases:
For node with left and right all equals NULL, we just need to return the node itself;
For a simplest tree [a, b, c], we must return the a b c sequence.
The rule is that, a is the first node of the flattened list, and the left child b is the next and the right child c is the last.
Then, we expand it to general cases: [root, left-tree, right-tree]
1. root should always be the first node.
2. the flatterned left-tree is the next of root.
3. the flatterned right-tree is the next of the last node of the flatterned left-tree.
So, we can see that the need variables are the first and last node of the flatterned list.
How to return two variables in one function? We can return the first node and use an extra function argument to achieve this.
Code:
View Code
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
Analysis:
Solution to tree problem is always recursive. So is this problem. First let's see some basic cases:
For node with left and right all equals NULL, we just need to return the node itself;
For a simplest tree [a, b, c], we must return the a b c sequence.
The rule is that, a is the first node of the flattened list, and the left child b is the next and the right child c is the last.
Then, we expand it to general cases: [root, left-tree, right-tree]
1. root should always be the first node.
2. the flatterned left-tree is the next of root.
3. the flatterned right-tree is the next of the last node of the flatterned left-tree.
So, we can see that the need variables are the first and last node of the flatterned list.
How to return two variables in one function? We can return the first node and use an extra function argument to achieve this.
Code:
/** * Definition for binary tree * 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 *h; if (root != NULL) ftHelper(root, &h); } TreeNode *ftHelper(TreeNode* node, TreeNode **llast) { TreeNode *last = node, *first = node; *llast = node; if (node->left == NULL && node->right == NULL) return node; TreeNode *l = node->left, *r = node->right, *h; if (l != NULL) { node->right = ftHelper(l, &last); node->left = NULL; } if (r != NULL) { last->right = ftHelper(r, &h); last = h; } *llast = last; return first; } };
View Code
相关文章推荐
- LeetCode题解-114-Flatten Binary Tree to Linked List
- [LeetCode]题解(python):114-Flatten Binary Tree to Linked List
- LeetCode114—Flatten Binary Tree to Linked List
- leetcode_114_Flatten Binary Tree to Linked List
- leetcode 114 —— Flatten Binary Tree to Linked List
- LeetCode - 114 - Flatten Binary Tree to Linked List
- Leetcode 114 Flatten Binary Tree to Linked List
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
- LeetCode_114 Flatten Binary Tree to Linked List
- leetcode 114 . Flatten Binary Tree to Linked List
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- [leetcode 114] Flatten Binary Tree to Linked List
- LeetCode 114 Flatten Binary Tree to Linked List
- 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- leetcode 114 Flatten Binary Tree to Linked List
- 【LEETCODE】114-Flatten Binary Tree to Linked List
- leetcode 114 Flatten Binary Tree to Linked List
- Leetcode_114_Flatten Binary Tree to Linked List
- LeetCode 114 Flatten Binary Tree to Linked List