114. Flatten Binary Tree to Linked List

把左边=null, 把最左边遍历到null 保证已经flatten 然后再弄右边 然后把root.right跟左边连接,再把右边连接到root.right的最下面

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/36987/Straightforward-Java-Solution

 

 

 1 class Solution {
 2     public void flatten(TreeNode root) {
 3         if(root == null) return;
 4         TreeNode left = root.left;
 5         TreeNode right = root.right;
 6         root.left = null;
 7         
 8         flatten(right);
 9         flatten(left);
10         root.right = left;
11         TreeNode cur = root;
12         while(cur.right != null) cur = cur.right;
13         cur.right = right;
14         
15     }
16 }

 

posted @ 2018-09-21 23:03  jasoncool1  阅读(83)  评论(0编辑  收藏  举报