mirror of https://github.com/doocs/leetcode.git
30 lines
627 B
Java
30 lines
627 B
Java
/**
|
|
* Definition for a binary tree node.
|
|
* public class TreeNode {
|
|
* int val;
|
|
* TreeNode left;
|
|
* TreeNode right;
|
|
* TreeNode(int x) { val = x; }
|
|
* }
|
|
*/
|
|
class Solution {
|
|
private TreeNode prev;
|
|
|
|
public TreeNode convertBiNode(TreeNode root) {
|
|
TreeNode dummy = new TreeNode(0, null, root);
|
|
prev = dummy;
|
|
dfs(root);
|
|
return dummy.right;
|
|
}
|
|
|
|
private void dfs(TreeNode root) {
|
|
if (root == null) {
|
|
return;
|
|
}
|
|
dfs(root.left);
|
|
prev.right = root;
|
|
root.left = null;
|
|
prev = root;
|
|
dfs(root.right);
|
|
}
|
|
} |