中序
class Solution {
List<Integer> result = new ArrayList<>();
public int kthSmallest(TreeNode root, int k) {
dfs(root);
return result.get(k - 1);
}
void dfs(TreeNode root){
if(root != null){
dfs(root.left);
result.add(root.val);
dfs(root.right);
}
}
}
该博客介绍了如何使用深度优先搜索(DFS)实现二叉树的中序遍历,并通过中序遍历找到树中第k小的节点。算法实现过程中,先遍历左子树,将根节点值加入结果列表,再遍历右子树。这种方法有效地解决了在二叉树中查找特定排名节点的问题。
692

被折叠的 条评论
为什么被折叠?



