114. Flatten Binary Tree to Linked List

Posted gopanama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于114. Flatten Binary Tree to Linked List的主要内容,如果未能解决你的问题,请参考以下文章

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

!!!!!!114. Flatten Binary Tree to Linked List

LeetCode 114. Flatten Binary Tree to Linked List