114. Flatten Binary Tree to Linked List

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if(root==null) return;
flatten(root.left);

TreeNode tmpL=root.left;
if(tmpL!=null){
TreeNode tmpR=root.right;
root.left=null;
root.right=tmpL;
tmpL.left=null;
while(tmpL.right!=null) tmpL=tmpL.right;
tmpL.right=tmpR;
}
flatten(root.right);
}
}

0%