-
[LeetCode] Binary Tree Inorder Traversal알고리즘 2019. 11. 26. 17:37
문제
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
Apporach
Inorder traversal: 왼쪽 자식-> 자신 -> 오른쪽 자식 순으로 트리 탐색
Recursive로 풀면 직관적이고 함수 스택 말고 실제 stack을 활용해 반복문으로 풀 수도 있다.
Code
Recursive: https://github.com/chi3236/algorithm/blob/master/LeetCode_BinaryTreeInorderTraversal_recursive.cpp
Stack: https://github.com/chi3236/algorithm/blob/master/LeetCode_BinaryTreeInorderTraversal_stack.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Symmetric Tree (0) 2019.11.28 [LeetCode] Validate Binary Search Tree (0) 2019.11.27 [LeetCode] Decode Ways (0) 2019.11.25 [LeetCode] Merge Sorted Array (0) 2019.11.22 [LeetCode] Largest Rectangle in Histogram (0) 2019.11.20