We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
源代码:
func constructBinaryTree(array []int) *TreeNode { var root *TreeNode nodes := make([]*TreeNode, len(array)) // 初始化二叉树节点 for i := 0; i < len(nodes); i++ { var node *TreeNode if array[i] != -1 { node = &TreeNode{Val: array[i]} } nodes[i] = node if i == 0 { root = node } } // 串联节点 for i := 0; i*2+2 < len(array); i++ { if nodes[i] != nil { nodes[i].Left = nodes[i*2+1] nodes[i].Right = nodes[i*2+2] } } return root }
这里最后串联节点的部分感觉不对,这样的话会漏掉节点,感觉可以写成
for i := 0; i < len(arr); i++ { if i*2+1 < len(arr) { nodes[i].Left = nodes[i*2+1] } if i*2+2 < len(arr) { nodes[i].Right = nodes[i*2+2] } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
源代码:
这里最后串联节点的部分感觉不对,这样的话会漏掉节点,感觉可以写成
The text was updated successfully, but these errors were encountered: