Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes InterviewBytes/Flatten2.java Secret
Created Jun 13, 2017

Embed
What would you like to do?
Flatten Binary tree 2
package com.interviewbytes.trees;
public class Flatten2 {
private TreeNode prev = null;
public void flatten(TreeNode root) {
if (root == null) return;
flatten(root.right);
flatten(root.left);
root.right = prev;
root.left = null;
prev = root;
}
}
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.