fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. class Node
  6. {
  7. public Node Left { get; protected set; }
  8. public Node Right { get; protected set; }
  9. public Node Parent { get; protected set; }
  10. public Node Next { get; set; }
  11.  
  12. public int Value { get; set; }
  13.  
  14. public Node(int val)
  15. {
  16. Value = val;
  17. }
  18.  
  19. public Node(int val, Node l, Node r) : this(val)
  20. {
  21. SetNodes(l, r);
  22. }
  23.  
  24. public void SetNodes(Node l, Node r)
  25. {
  26. if (l != null) l.Parent = this;
  27. if (r != null) r.Parent = this;
  28.  
  29. Left = l;
  30. Right = r;
  31. }
  32.  
  33. public void SetAsParent(Node p)
  34. {
  35. if (p.Left == null)
  36. p.Left = this;
  37. else if (p.Right == null)
  38. p.Right = this;
  39. else
  40. throw new Exception("Busy");
  41. }
  42. }
  43.  
  44. static void Main(string[] args)
  45. {
  46. Node leaf1 = new Node(1);
  47. Node leaf2 = new Node(2);
  48. Node leaf3 = new Node(3);
  49. Node mid1 = new Node(4, leaf1, null);
  50. Node mid2 = new Node(5, leaf2, leaf3);
  51. Node root = new Node(6, mid1, mid2);
  52.  
  53. root.Next = root.Left;
  54. Link(root);
  55.  
  56. for (var r = root; r != null; r = r.Next)
  57. {
  58. Console.Write(r.Value + " ");
  59. }
  60. Console.Write("\r\n");
  61. }
  62.  
  63. static void Link(Node n)
  64. {
  65. if (n == null)
  66. return;
  67.  
  68. if (n.Left != null)
  69. n.Left.Next = n.Right;
  70.  
  71. if (n.Right != null)
  72. n.Right.Next = n.Next.Left;
  73.  
  74. Link(n.Left);
  75. Link(n.Right);
  76. }
  77. }
Success #stdin #stdout 0.04s 23880KB
stdin
Standard input is empty
stdout
6  4  5  1