返回介绍

lcci / 17.12.BiNode / README_EN

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

17.12. BiNode

中文文档

Description

The data structure TreeNode is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to convert a binary search tree (implemented with TreeNode) into a single linked list. The values should be kept in order and the operation should be performed in place (that is, on the original data structure).

Return the head node of the linked list after converting.

Note: This problem is slightly different from the original one in the book.

 

Example:


Input:  [4,2,5,1,3,null,6,0]

Output:  [0,null,1,null,2,null,3,null,4,null,5,null,6]

Note:

  • The number of nodes will not exceed 100000.

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 convertBiNode(self, root: TreeNode) -> TreeNode:
    def dfs(root):
      if root is None:
        return
      nonlocal prev
      dfs(root.left)
      prev.right = root
      root.left = None
      prev = root
      dfs(root.right)

    dummy = TreeNode(val=0, right=root)
    prev = dummy
    dfs(root)
    return dummy.right
/**
 * 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;

  public TreeNode convertBiNode(TreeNode root) {
    TreeNode dummy = new TreeNode(0, null, root);
    prev = dummy;
    dfs(root);
    return dummy.right;
  }

  private void dfs(TreeNode root) {
    if (root == null) {
      return;
    }
    dfs(root.left);
    prev.right = root;
    root.left = null;
    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* convertBiNode(TreeNode* root) {
    TreeNode* dummy = new TreeNode(0, nullptr, root);
    prev = dummy;
    dfs(root);
    return dummy->right;
  }

  void dfs(TreeNode* root) {
    if (!root) return;
    dfs(root->left);
    prev->right = root;
    root->left = nullptr;
    prev = root;
    dfs(root->right);
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func convertBiNode(root *TreeNode) *TreeNode {
  dummy := &TreeNode{Val: 0, Right: root}
  prev := dummy
  var dfs func(root *TreeNode)
  dfs = func(root *TreeNode) {
    if root == nil {
      return
    }
    dfs(root.Left)
    prev.Right = root
    root.Left = nil
    prev = root
    dfs(root.Right)
  }
  dfs(root)
  return dummy.Right
}

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

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

发布评论

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