标签:
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
此题的大概意思就是原地的把二叉树按先序遍历序列串起来。比如上图的二叉树的先序遍历序列为1,2,3,4,5,6.然后用树结构串联起来就是下面那个二叉树。
public void flatten(TreeNode root) {
if (root == null) return;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = null;
flatten(left);
flatten(right);
root.right = left;
TreeNode cur = root;
while (cur.right != null) cur = cur.right;
cur.right = right;
}感觉树的一般处理模式就是:
处理left; 处理right; 跟root拼接起来
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode-Flatten Binary Tree to Linked List
标签:
原文地址:http://blog.csdn.net/my_jobs/article/details/47667289