遍历二叉树,通过辅助方法比较两个值的大小,把结果记录下来。
1
using System;
2
using System.Collections.Generic;
3
4
namespace Algorithm
5
{
6
public class Node
7
{
8
private int _value;
9
private Node _leftnode;
10
private Node _rightnode;
11
public int Value
12
{
13
get
14
{
15
return this._value;
16
}
17
set
18
{
19
this._value = value;
20
}
21
}
22
public Node LeftNode
23
{
24
get
25
{
26
return this._leftnode;
27
}
28
set
29
{
30
this._leftnode = value;
31
}
32
}
33
public Node RightNode
34
{
35
get
36
{
37
return this._rightnode;
38
}
39
set
40
{
41
this._rightnode = value;
42
}
43
}
44
public Node(int value)
45
{
46
this._value = value;
47
this._rightnode = null;
48
this._leftnode = null;
49
}
50
public Node(int value,Node left,Node right)
51
{
52
this._value = value;
53
this._rightnode = left;
54
this._leftnode = right;
55
}
56
}
57
public class BinaryTree
58
{
59
private Node _root;
60
public Node Root
61
{
62
get
63
{
64
return this._root;
65
}
66
set
67
{
68
this._root = value;
69
}
70
}
71
public BinaryTree(Node node)
72
{
73
this._root = node;
74
}
75
private Node CompareNode(Node n,Node m)
76
{
77
return n.Value > m.Value ? n : m;
78
}
79
public Node result;
80
81
public void FindNode(Node node,int intCompare)
82
{
83
if ((node != null) && (intCompare > node.Value))
84
{
85
if (result == null)
86
{
87
result = node;
88
}
89
else
90
{
91
result = CompareNode(result, node);
92
}
93
}
94
if (node.LeftNode != null)
95
{
96
FindNode(node.LeftNode, intCompare);
97
}
98
if (node.RightNode != null)
99
{
100
FindNode(node.RightNode, intCompare);
101
}
102
}
103
}
104
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104
