简体   繁体   English

在二叉树而不是BST中查找节点的深度

[英]finding depth of node in a binary tree not BST

I have a binary tree not bst ,I need to find the depth of a node in that binary tree Is there any other way to achieve other than level order traversal using some dilimeter to main the count of level . 我有一个不是二叉树的二叉树,我需要在那个二叉树中找到一个节点的深度。除了使用某种测径仪来进行水准计数的水准顺序遍历之外,还有其他方法可以实现。

As input I have the root node of the tree and one of the node of the tree for which i need find the depth. 作为输入,我有树的根节点和我需要为其找到深度的树的节点之一。

I want to have some recursive way to find this 我想以某种递归的方式找到这个

如果您不想执行BFS,则可以执行DFS(也可以递归执行)。

pseudo-code for DFS function, the first call will be DFS(root) . DFS函数的伪代码,第一个调用将是DFS(root)

DFS(node v, integer d)
  visited[v] = true
  depth[v] = d

  for each u such that u is adjacent to v
    if visited[u] == false
      DFS(u, d+1)

尝试在递归函数中传递一个附加参数以指示深度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM