每日一题——后继者

简介: 每日一题——后继者

面试题 04.06. 后继者

题目描述:

题解:

直接进行中序遍历(左,根,右),按照中序遍历的顺序,依次加入到切片中,然后遍历这个切片去对比p即可:

func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
  ans:=make([]TreeNode,0)
  ans = ldr(root,&ans)
  for i := 0; i < len(ans)-1; i++ {
    if ans[i].Val==p.Val {
      return &ans[i+1]
    }
  }
  return nil
}
func ldr(node *TreeNode,ans *[]TreeNode) []TreeNode {
  if node==nil {
    return nil
  }
  ldr(node.Left,ans)
  *ans = append(*ans, *node)
  ldr(node.Right,ans)
  return *ans
}

提交结果:

相关文章
|
6天前
每日一题——移动零
每日一题——移动零
|
10月前
LeetCode】每日一题(4)
LeetCode】每日一题(4)
27 0
|
10月前
【LeetCode】每日一题(2)
【LeetCode】每日一题(2)
43 0
|
机器学习/深度学习 测试技术
AcWing - 寒假每日一题2023(DAY 16——DAY 20)
AcWing - 寒假每日一题2023(DAY 16——DAY 20)
|
人工智能 Java C++
AcWing - 寒假每日一题2023(DAY 1——DAY 5)
AcWing - 寒假每日一题2023(DAY 1——DAY 5)
|
存储 人工智能 BI
AcWing - 寒假每日一题2023(DAY 11——DAY 15)
AcWing - 寒假每日一题2023(DAY 11——DAY 15)
|
存储 人工智能 算法
AcWing - 寒假每日一题2023(DAY 6——DAY 10)
AcWing - 寒假每日一题2023(DAY 6——DAY 10)
每日一题:Leetcode283 移动零
每日一题:Leetcode283 移动零
|
算法
LeetCode每日一题(24)——后继者
后继者 1.题目 2.示例 3.思路 4.代码