遍历二叉树,通过辅助方法比较两个值的大小,把结果记录下来。
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
}
using System;2
using System.Collections.Generic;3

4
namespace Algorithm5
{ 6
public class Node7
{8
private int _value;9
private Node _leftnode;10
private Node _rightnode;11
public int Value12
{13
get14
{15
return this._value;16
}17
set18
{19
this._value = value;20
}21
}22
public Node LeftNode23
{24
get25
{26
return this._leftnode;27
}28
set29
{30
this._leftnode = value;31
}32
}33
public Node RightNode34
{35
get36
{37
return this._rightnode;38
}39
set40
{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 BinaryTree58
{59
private Node _root;60
public Node Root61
{62
get63
{64
return this._root;65
}66
set67
{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
else90
{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
}
