美文网首页
LeetCode之Binary Tree Inorder Tra

LeetCode之Binary Tree Inorder Tra

作者: 糕冷羊 | 来源:发表于2021-09-13 18:10 被阅读0次

问题:



方法:
中序遍历,看码吧。

package com.eric.leetcode

class BinaryTreeInorderTraversal {
    private val result = mutableListOf<Int>()

    fun inorderTraversal(root: TreeNode?): List<Int> {
        result.clear()
        dfs(root)
        return result
    }

    private fun dfs(root: TreeNode?) {
        if (root == null) {
            return
        }
        dfs(root.left)
        result.add(root.`val`)
        dfs(root.right)
    }
}

有问题随时沟通

具体代码实现可以参考Github

相关文章

网友评论

      本文标题:LeetCode之Binary Tree Inorder Tra

      本文链接:https://www.haomeiwen.com/subject/sxbfgltx.html