Ejemplo n.º 1
0
 /// <summary>
 /// 递归遍历
 /// </summary>
 /// <param name="root"></param>
 /// <param name="arr"></param>
 private void inorderTraversal(TreeNode root, List <int> arr)
 {
     if (root == null)
     {
         return;
     }
     inorderTraversal(root.left, arr);  // 增加节点 左侧
     arr.Add(root.val);                 // 增加节点 自身
     inorderTraversal(root.right, arr); // 增加节点 右侧
 }
Ejemplo n.º 2
0
        public List <int> inorder(TreeNode root, List <int> arr)
        {
            if (root == null)
            {
                return(arr);
            }

            inorder(root.left, arr);
            arr.Add(root.val);
            inorder(root.right, arr);
            return(arr);
        }
Ejemplo n.º 3
0
        public override bool Test(System.Diagnostics.Stopwatch sw)
        {
            bool isSuccess = true;
            int  num       = 0;

            TreeNode node = new TreeNode();

            Codec    obj  = new Codec();
            string   data = obj.serialize(node);
            TreeNode des  = obj.deserialize(data);

            return(isSuccess);
        }
Ejemplo n.º 4
0
        public override bool Test(System.Diagnostics.Stopwatch sw)
        {
            bool        isSuccess   = true;
            TreeNode    treeRoot    = TreeNode.CreateBST(new int[] { 7, 3, 15, 9, 20 });//???  { 7, 3, 15, null, null, 9, 20 }
            BSTIterator bSTIterator = new BSTIterator(treeRoot);

            System.Diagnostics.Debug.Assert(bSTIterator.next() == 3);        // 返回 3
            System.Diagnostics.Debug.Assert(bSTIterator.next() == 7);        // 返回 7
            System.Diagnostics.Debug.Assert(bSTIterator.hasNext() == true);  // 返回 True
            System.Diagnostics.Debug.Assert(bSTIterator.next() == 9);        // 返回 9
            System.Diagnostics.Debug.Assert(bSTIterator.hasNext() == true);  // 返回 True
            System.Diagnostics.Debug.Assert(bSTIterator.next() == 15);       // 返回 15
            System.Diagnostics.Debug.Assert(bSTIterator.hasNext() == true);  // 返回 True
            System.Diagnostics.Debug.Assert(bSTIterator.next() == 20);       // 返回 20
            System.Diagnostics.Debug.Assert(bSTIterator.hasNext() == false); // 返回 False
            return(isSuccess);
        }
Ejemplo n.º 5
0
        public override bool Test(System.Diagnostics.Stopwatch sw)
        {
            bool     isSuccess = true;
            int      checkresult;
            int      k;
            TreeNode node;

            node        = TreeNode.Create(new string[] { "5", "3", "6", "2", "4", null, null, "1" });
            k           = 3;
            checkresult = 3;
            isSuccess  &= KthSmallest(node, k) == checkresult;

            node        = TreeNode.Create(new string[] { "3", "1", "4", null, "2" });
            k           = 1;
            checkresult = 1;
            isSuccess  &= KthSmallest(node, k) == checkresult;
            System.Diagnostics.Debug.Assert(isSuccess);

            return(isSuccess);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  二叉搜索树中第K小的元素
        /// 通过构造 BST 的中序遍历序列,则第 k-1 个元素就是第 k 小的元素。
        ///
        /// Your runtime beats 98.95 % of csharp submissions
        /// Your memory usage beats 7.37 % of csharp submissions
        ///
        /// </summary>
        /// <param name="root"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        public int KthSmallest_MySelf(TreeNode root, int k)
        {
            List <TreeNode> nodeList = new List <TreeNode>();

            TreeNode.GetNodeList(root, nodeList);
            return(nodeList[k - 1].val);

            /*
             * /// 自己最初的算法,效率很低。
             * /// Your runtime beats 22.11 % of csharp submissions
             * /// Your memory usage beats 5.27 % of csharp submissions(29.3 MB)
             * List<int> nodeValues = new List<int>();
             * foreach(TreeNode node in nodeList)
             * {
             *  if(node != null)
             *      nodeValues.Add(node.val);
             * }
             * nodeValues.Sort();
             * return nodeValues[k-1];
             */
        }
Ejemplo n.º 7
0
 // Encodes a tree to a single string.
 public string serialize(TreeNode root)
 {
     return("");
 }
Ejemplo n.º 8
0
        /// 官方答案,其实和自己的一样。
        public int KthSmallest(TreeNode root, int k)
        {
            List <int> nums = inorder(root, new List <int>());

            return(nums[k - 1]);
        }
Ejemplo n.º 9
0
 public BSTIterator(TreeNode root)
 {
     idx = 0;
     arr = new List <int>();
     inorderTraversal(root, arr);
 }