Recursion

  1. selfcontained recursion
  2. global variables outside of recursion

Recursion Design 

  1. Whenever reach to a qualified node, record the node reference and level for that node
  2. if meet next qualified node, compare the level, if larger, refresh the global node reference and level.
  3. Recursively do 1 and 2 recursively for left tree and right tree

 


Data Structure 

复制代码
    public class AVLTreeNode
    {
        public int data
        {
            get;
            set;
        }

        public AVLTreeNode leftChild
        {
            get;
            set;
        }

        public AVLTreeNode rightChild
        {
            get;
            set;
        }

        public int height
        {
            get;
            set;
        }

        public AVLTreeNode(int data)
        {
            this.data = data;
            this.height = 1;
        }
    }
复制代码

Source Code

复制代码
   public class Result
    {
        public int MaxHight
        {
            get;
            set;
        }

        public AVLTreeNode ResultNode
        {
            get;
            set;
        }
    }

        // Find deepest left node
        public void FindDeepestLeftNode(AVLTreeNode node, bool isLeft, int height, Result result)
        {
            if (node == null)
            {
                return;
            }

            if (isLeft)
            {
                if (node.leftChild == null && node.rightChild == null && height > result.MaxHight)
                {
                    result.ResultNode = node;
                    result.MaxHight = height;
                }
            }

            FindDeepestLeftNode(node.leftChild, true, height + 1, result);
            FindDeepestLeftNode(node.rightChild, false, height + 1, result);
        }

        public AVLTreeNode GetDeepestLeftNode()
        {
            Result result = new Result()
            {
                MaxHight = 0,
                ResultNode = null
            };

            FindDeepestLeftNode(root, true, 0, result);
            return result.ResultNode;
        }
复制代码

Complexity

Time complexity is O(N)

Space complexity is O(N)

posted on   xuyanran  阅读(106)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示