JS-알고리즘 Leetcode(Easy)-Binary Tree Inorder Traversal
포스트
취소

JS-알고리즘 Leetcode(Easy)-Binary Tree Inorder Traversal

Question

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

대충 해석

주어진 이진트리를 중위순회한 값을 반환하세요.

Example

1
2
3
4
5
6
7
8
9
10
11
#1
Input: root = [1,null,2,3]
Output: [1,3,2]

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

#3
Input: root = [1]
Output: [1]

Solution

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
// 94. Binary Tree Inorder Traversal
/**
 * 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) {
    
    let result = [];
    const helper = function(nodes) {
        
        if(!nodes) {
            return result;
        }
        
        helper(nodes.left);
        result.push(nodes.val);
        helper(nodes.right);
        
        return nodes;
    }
    
    helper(root);
    
    return result;
};
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

JAVA-알고리즘 Leetcode(Medium)-Design Browser History

TS-알고리즘 Leetcode(Easy)-Single Number