您的位置:首页 > 其它

递归---Flatten Binary Tree to Linked List

2016-04-03 21:51 429 查看
Flatten a binary tree to a fake “linked list” in pre-order traversal.

Here we use the right pointer in TreeNode as the next pointer in ListNode.

public class Solution {
private TreeNode lastNode = null;

public void flatten(TreeNode root) {
if (root == null) {
return;
}

if (lastNode != null) {
lastNode.left = null;
lastNode.right = root;
}

lastNode = root;
TreeNode right = root.right;
flatten(root.left);
flatten(right);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: