首页 > 其他 > 详细

114. Flatten Binary Tree to Linked List

时间:2018-09-22 00:35:32      阅读:167      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!