Created
January 28, 2015 02:18
-
-
Save the-fejw/7bfb060307738ad04d92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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