LintCode: Flatten Binary Tree to Linked List

简介:

C++

Traverse

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: a TreeNode, the root of the binary tree
17      * @return: nothing
18      */
19     void flatten(TreeNode *root) {
20         if(!root) return;
21         vector<TreeNode*> allNodes;
22         preorder(root, allNodes);
23         int n = allNodes.size();
24         for(int i=0; i<n-1; i++) {
25             allNodes[i]->left = NULL;
26             allNodes[i]->right = allNodes[i+1];
27         }
28         allNodes[n-1]->left = allNodes[n-1]->right = NULL;
29     }
30     
31     void preorder(TreeNode *root, vector<TreeNode*> &allNodes) {
32         if(!root) return;
33         allNodes.push_back(root);
34         preorder(root->left, allNodes);
35         preorder(root->right, allNodes);
36     }
37 };
复制代码

C++

Divide-Conquer

Recursive

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: a TreeNode, the root of the binary tree
17      * @return: nothing
18      */
19     void flatten(TreeNode *root) {
20         helper(root);
21     }
22     
23     TreeNode* helper(TreeNode *root) {
24         if (root == NULL) {
25             return NULL;
26         }
27         if (root->left == NULL && root->right == NULL) {
28             return root;
29         }
30         if (root->left == NULL) {
31             return helper(root->right);
32         }
33         if (root->right == NULL) {
34             root->right = root->left;
35             root->left = NULL;//!important
36             return helper(root->right);
37         }
38         //Divide
39         TreeNode *leftLastNode = helper(root->left);
40         TreeNode *rightLastNode = helper(root->right);
41         
42         //Conquer
43         leftLastNode->right = root->right;
44         root->right = root->left;
45         root->left = NULL;//!important
46         return rightLastNode;
47     }
48 };
复制代码

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5012685.html,如需转载请自行联系原作者

相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
43 1
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode 430. Flatten a Multilevel Doubly Linked
使用循环,从头开始遍历,将子链看成一个整体,将整体向上移动一层。 继续向后遍历,如果遇到子链,将其看作一层,将其整体向上移动,如此循环下去,直至结束。
73 0
LeetCode 430. Flatten a Multilevel Doubly Linked
LeetCode 106. Construct Binary Tree
给定一颗二叉树的中序和后续遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
73 0
LeetCode 106. Construct Binary Tree
LeetCode 105. Construct Binary Tree
给定一颗二叉树的前序和顺序遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
50 0
LeetCode 105. Construct Binary Tree
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现