Redian新闻
>
find treenode with two indegrees
avatar
find treenode with two indegrees# JobHunting - 待字闺中
k*t
1
看到一个题和一个答案。不太理解。
1.What does it mean that "tree representation is in Linked list format"?
Is there a standard way to represent a tree with a linked list? Or does it
just refer to the fact that TreeNode has left and right child link/pointers.
2. "When we thread a binary tree while doing preorder traversal, it wont
result in any cycle."
Is there a standard way to "thread' a binary tree? There is no "cycle"
concept at all in normal tree traversal, isn't it?
=======================================
Suppose there is a binary tree having millions of nodes and by mistake one
node has two indegree........(i.e. There becomes a cycle/loop in that tree .
..u have to find that node which is having two indegree....
Constraint is no extra memory can be used and tree representation is in
Linked list format...
for example..
............0
........../..\
.........1...2
......../.\./.\
.......3...4...5
....../.\...\
.....6...7...8
node 4 is having two indegree....we have to find that node....
===============================================================
When we thread a binary tree while doing preorder traversal, it wont result
in any cycle. But it leads to a cycle in this case. We can make use of this
fact.
preorder:
i) Print node.
ii) If node->left is not NULL search right most node in the left subtree of
the current node and make the current nodes right child as the right child
of the found node.
iii) If node->left is NULL, go to the right subtree, ie current node=
current node->right.
This will yield a acyclic directed graph in an ideal case, but a cyclic
graph in case the indegree of any node is > 1.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。