• 找出二叉树中所有值比某个给定整数小的节点中值最大的那个节点


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

      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}
  • 相关阅读:
    蒙特卡洛法(随即取样法) 数模 笔记
    【数模学习】Matlab 符号微积分 计算微分、雅可比矩阵、不定积分与定积分、求解微分方程
    Length of Last Word
    基于视频深度学习的人物行为识别 资料汇总
    3S比赛预定
    求解一元多次方程 牛顿迭代法
    LeetCode | Climbing Stairs
    LeetCode | Palindrome Number
    LeetCode | Merge Sorted Array
    LeetCode | Valid Palindrome
  • 原文地址:https://www.cnblogs.com/zhangz/p/1301506.html
Copyright © 2020-2023  润新知