Skip to content

Commit

Permalink
中序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
gdis5251 committed Jul 23, 2021
1 parent ba90c1c commit 1f8417b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 精选/04.06.后继者/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

var resArr = make([]*TreeNode, 0)

func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
if root == nil {
return nil
}

midOrder(root)

for index := range resArr {
if resArr[index] == p && index < len(resArr) - 1 {
return resArr[index + 1]
}
}
return nil
}

func midOrder(root *TreeNode) {
if root == nil {
return
}

midOrder(root.Left)
resArr = append(resArr, root)
midOrder(root.Right)
}

0 comments on commit 1f8417b

Please sign in to comment.