标签: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