File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=114 lang=javascript
3+ *
4+ * [114] Flatten Binary Tree to Linked List
5+ */
6+ /**
7+ * Definition for a binary tree node.
8+ * function TreeNode(val) {
9+ * this.val = val;
10+ * this.left = this.right = null;
11+ * }
12+ */
13+ function preorderTraversal ( root ) {
14+ if ( ! root ) return [ ] ;
15+
16+ return [ root ]
17+ . concat ( preorderTraversal ( root . left ) )
18+ . concat ( preorderTraversal ( root . right ) ) ;
19+ }
20+ /**
21+ * @param {TreeNode } root
22+ * @return {void } Do not return anything, modify root in-place instead.
23+ */
24+ var flatten = function ( root ) {
25+ if ( root === null ) return root ;
26+ const res = preorderTraversal ( root ) ;
27+
28+ let curPos = 0 ;
29+ let curNode = res [ 0 ] ;
30+
31+ while ( curNode = res [ curPos ] ) {
32+ curNode . left = null ;
33+ curNode . right = res [ ++ curPos ] ;
34+ }
35+ } ;
You can’t perform that action at this time.
0 commit comments