-
[LeetCode] Maximum Depth of Binary Tree알고리즘 2019. 12. 3. 01:54
문제
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
return its depth = 3.
Approach
DFS를 활용한 inorder traversal로 depth를 측정하거나
BFS를 활용한 level order traversal로 depth를 측정하면 된다.
Code
DFS: https://github.com/chi3236/algorithm/blob/master/LeetCode_MaximumDepthOfBinaryTree_DFS.cpp
BFS: https://github.com/chi3236/algorithm/blob/master/LeetCode_MaximumDepthOfBinaryTree_BFS.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Convert Sorted Array to Binary Search Tree (0) 2019.12.04 [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal (0) 2019.12.04 [LeetCode] Binary Tree Zigzag Level Order Traversal (0) 2019.12.02 [LeetCode] Binary Tree Level Order Traversal (0) 2019.12.02 [LeetCode] Symmetric Tree (0) 2019.11.28