zoukankan      html  css  js  c++  java
  • 找出二叉树中所有值比某个给定整数小的节点中值最大的那个节点

    遍历二叉树,通过辅助方法比较两个值的大小,把结果记录下来。

      1using System;
      2using System.Collections.Generic;
      3
      4namespace 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}
  • 相关阅读:
    2015/8/3 接着跌
    2015/7/31 由于昨天上升缺乏量的支持,今天横盘;在箱体下边缘稍微买了一点---错误!;复文《揭秘主力坐庄流程 内幕超乎想象》,
    打包jar类库与使用jar类库
    java eclipse 监视选择指定变量
    2015/7/29 (高开,V形反转,各种指标背离——可惜没买进,填补空缺图形的心理分析)
    XP、win7下Excel 2007多窗口打开Excel的解决方法
    2015/7/28(总结昨天抄底操作失败-割肉自保)
    六首失传股诗教你如何抄底和逃顶
    2015/7/27 (主力流出-1200亿,上周五回踩,今天到底是震荡下行,还是红魔呢?——在周五成功逃顶,结果今天回调的时候被套!——教训!)
    java中byte数组与int,long,short间的转换
  • 原文地址:https://www.cnblogs.com/zhangz/p/1301506.html
Copyright © 2011-2022 走看看