Skip to content

Instantly share code, notes, and snippets.

@the-fejw
Created January 28, 2015 02:18
Show Gist options
  • Save the-fejw/7bfb060307738ad04d92 to your computer and use it in GitHub Desktop.
Save the-fejw/7bfb060307738ad04d92 to your computer and use it in GitHub Desktop.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
static TreeNode lastVisitedNode = null;
public void flatten(TreeNode root) {
if (root == null) {
return;
}
TreeNode right = root.right;
if (lastVisitedNode != null) {
lastVisitedNode.left = null;
lastVisitedNode.right = root;
}
lastVisitedNode = root;
flatten(root.left);
flatten(right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment