0%

Javascript(leetcode#94) Binary Tree Inorder Traversal

Difficult:Easy

題目

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

翻譯

給定二叉樹的根,返回其節點值的中序遍歷。

範例

Example 1:
example

1
2
Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:

1
2
Input: root = []
Output: []

Example 3:

1
2
Input: root = [1]
Output: [1]

解題思路

1.利用遞迴解

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var inorderTraversal = function (root) {
let arr = [];
traversal(root, arr);

return arr;
};

function traversal(node, arr) {

if (node == null || node == undefined) return;
traversal(node.left);
arr.push(node.val);
traversal(node.right);
}