114. Flatten Binary Tree to Linked List(unsolved)
来源:互联网 发布:淘宝联盟怎么一起结算 编辑:程序博客网 时间:2024/06/06 10:48
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
解答:这道题其实思路比较简单,但是可能编程上要考虑很多意外情况。
简单来说,就是把左子节点中最靠右的叶子节点指向右子节点,然后把整个左子节点挪到右边。然后root往右子节点走一步。如此递归下去。
/** * 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 change(TreeNode* root) { while(root) { if(root->left&&root->right) { TreeNode* t=root->left; while(t->right!=NULL) { t=t->right; } t->right=root->right; } if(root->left) root->right=root->left; root->left=NULL; root=root->right; } } void flatten(TreeNode* root) { change(root); }};
0 0
- 114. Flatten Binary Tree to Linked List(unsolved)
- 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
- MySQL 输入中文数据
- BZOJ 3277&&3473 广义后缀自动机
- 2016蓝桥杯C/C++省赛 剪邮票(深搜DFS+暴力)
- jimu Mirror的使用(android快速手机预览xml界面)
- CPA、CPS、CPM、CPT、CPC 是什么
- 114. Flatten Binary Tree to Linked List(unsolved)
- AOP
- xargs用法详解
- C++内存机制
- 自定义控件
- spring与mybatis三种整合方法
- 原生Android也能做Web开发了
- HBuilder mui 手势事件
- 随手记->Glide初始化