Leetcode Note: Go - Maximum Depth of Binary Tree
Maximum Depth of Binary Tree - LeetCode
https://leetcode.com/problems/maximum-depth-of-binary-tree/
- Go 言語で取り組んだメモ
所感
- わからん
回答
recursive and iterative using golang - LeetCode Discuss
https://leetcode.com/problems/maximum-depth-of-binary-tree/discuss/854002/recursive-and-iterative-using-golang
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(x int, y int) int {
if x > y {
return x
}
return y
}
- max 関数を実装値して、 root の Left, Right を再帰的に確認して、 root のぶん +1 すれば良い