返回介绍

lcci / 04.06.Successor / README_EN

发布于 2024-06-17 01:04:43 字数 5151 浏览 0 评论 0 收藏 0

04.06. Successor

中文文档

Description

Write an algorithm to find the "next" node (i.e., in-order successor) of a given node in a binary search tree.

Return null if there's no "next" node for the given node.

Example 1:


Input: root = [2,1,3], p = 1



  2

 / \

1   3



Output: 2

Example 2:


Input: root = [5,3,6,2,4,null,null,1], p = 6



    5

   / \

  3   6

   / \

  2   4

 /   

1



Output: null

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, x):
#     self.val = x
#     self.left = None
#     self.right = None


class Solution:
  def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode:
    def dfs(root):
      if root is None:
        return
      dfs(root.left)
      nonlocal ans, prev
      if prev == p:
        ans = root
      prev = root
      dfs(root.right)

    ans = prev = None
    dfs(root)
    return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
 */
class Solution {
  private TreeNode prev;
  private TreeNode p;
  private TreeNode ans;

  public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
    prev = null;
    ans = null;
    this.p = p;
    dfs(root);
    return ans;
  }

  private void dfs(TreeNode root) {
    if (root == null) {
      return;
    }
    dfs(root.left);
    if (prev == p) {
      ans = root;
    }
    prev = root;
    dfs(root.right);
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  TreeNode* prev;
  TreeNode* p;
  TreeNode* ans;

  TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
    this->p = p;
    dfs(root);
    return ans;
  }

  void dfs(TreeNode* root) {
    if (!root) return;
    dfs(root->left);
    if (prev == p) ans = root;
    prev = root;
    dfs(root->right);
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
  var prev, ans *TreeNode
  var dfs func(root *TreeNode)
  dfs = func(root *TreeNode) {
    if root == nil {
      return
    }
    dfs(root.Left)
    if prev == p {
      ans = root
    }
    prev = root
    dfs(root.Right)
  }
  dfs(root)
  return ans
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *   this.val = val;
 *   this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @return {TreeNode}
 */
var inorderSuccessor = function (root, p) {
  if (root == null) {
    return root;
  }
  const { val, left, right } = root;
  const res = inorderSuccessor(left, p);
  if (res != null) {
    return res;
  }
  if (val > p.val) {
    return root;
  }
  return inorderSuccessor(right, p);
};

Solution 2

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
    stack<TreeNode*> stk;
    TreeNode* cur = root;
    while (cur != nullptr || !stk.empty()) {
      if (cur == nullptr) {
        cur = stk.top();
        stk.pop();
        if (cur->val > p->val) {
          return cur;
        }
        cur = cur->right;
      } else {
        stk.push(cur);
        cur = cur->left;
      }
    }
    return cur;
  }
};
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *   this.val = val;
 *   this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @return {TreeNode}
 */
var inorderSuccessor = function (root, p) {
  const stack = [];
  let cur = root;
  while (cur != null || stack.length !== 0) {
    if (cur == null) {
      cur = stack.pop();
      if (cur.val > p.val) {
        return cur;
      }
      cur = cur.right;
    } else {
      stack.push(cur);
      cur = cur.left;
    }
  }
  return cur;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文