Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题7:重建二叉树 #56

Open
SunshlnW opened this issue Jul 27, 2019 · 6 comments
Open

面试题7:重建二叉树 #56

SunshlnW opened this issue Jul 27, 2019 · 6 comments

Comments

@SunshlnW
Copy link

SunshlnW commented Jul 27, 2019

66-67行代码
if(rootInorder == endInorder && *rootInorder != rootValue)
throw std::exception("Invalid input.");
这句没有用吧!

@CoderLeonidas
Copy link

66-67行代码
if(rootInorder == endInorder && *rootInorder != rootValue)
throw std::exception("Invalid input.");
这句没有用吧!

只有一个节点且这个节点还和记录的根节点rootValue不一致,就是非法输入的意思吧

@L-Xu-L
Copy link

L-Xu-L commented Feb 24, 2020

66-67行代码
if(rootInorder == endInorder && *rootInorder != rootValue)
throw std::exception("Invalid input.");
这句没有用吧!

判断前序遍历找到的根的值 跟中序遍历里的根的值 是否相等吧

@EternalWang
Copy link

66-67行代码
if(rootInorder == endInorder && *rootInorder != rootValue)
throw std::exception("Invalid input.");
这句没有用吧!

首先,这两句是在第61和62行。
个人认为应该把第61句if(rootInorder == endInorder && *rootInorder != rootValue)改为if(rootInorder > endInorder),处理在中序数组中未找到root的情况。

@gyzcool
Copy link

gyzcool commented Jan 16, 2021

66-67行代码
if(rootInorder == endInorder && *rootInorder != rootValue)
throw std::exception("Invalid input.");
这句没有用吧!

首先,这两句是在第61和62行。
个人认为应该把第61句if(rootInorder == endInorder && *rootInorder != rootValue)改为if(rootInorder > endInorder),处理在中序数组中未找到root的情况。

赞同!0.0 刚看到书的这块,觉得有点小问题。

@lurenxiao1998
Copy link

刚才看到这块也觉得有问题,61行改成if(rootInorder > endInorder)或者58行改成while(rootInorder < endInorder && *rootInorder != rootValue)。不然感觉没办法处理找不到根的情况。

@lurenxiao1998
Copy link

刚才看到这块也觉得有问题,61行改成if(rootInorder > endInorder)或者58行改成while(rootInorder < endInorder && *rootInorder != rootValue)。不然感觉没办法处理找不到根的情况。

虽然这个地方跑代码没有问题的,如果rootInorder > endInorder。那么在构建右子树的时候就会抛出异常。但是这个地方应该是写错了,不然太奇怪了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants