码迷,mamicode.com
首页 > 其他好文 > 详细

114. Flatten Binary Tree to Linked List

时间:2018-09-22 01:05:14      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:node   com   tco   code   color   java   tree   bsp   14.   

把左边=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 }

 

114. Flatten Binary Tree to Linked List

标签:node   com   tco   code   color   java   tree   bsp   14.   

原文地址:https://www.cnblogs.com/goPanama/p/9688944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!