-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path89-binary-tree-inorder-traversal.js
More file actions
44 lines (38 loc) · 988 Bytes
/
89-binary-tree-inorder-traversal.js
File metadata and controls
44 lines (38 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function (root) {
const r = [];
inorder(root, r);
return r;
};
function inorder(node, r) {
if (node) {
inorder(node.left, r);
r.push(node.val);
inorder(node.right, r);
}
}
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
// Input: root = [1, null, 2, 3];
// Output: [1,3,2]
// Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]
// Output: [4,2,6,5,7,1,3,9,8]
root = new TreeNode(1);
// root.left = null;
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
console.log(inorderTraversal(root));