示例#1
0
        public List <int> Test(TreeNodeIb a)
        {
            var s = new Stack <TreeNodeIb>();

            var r = new List <int>();

            TreeNodeIb lastVisited = null;

            while (s.Count != 0 || a != null)
            {
                if (a != null)
                {
                    s.Push(a);
                    a = a.left;
                }
                else
                {
                    // If the popped item has a right child and the right child is not
                    // processed yet, then make sure right child is processed before root
                    var p = s.Peek();
                    if (p.right != null && p.right != lastVisited)
                    {
                        a = p.right;
                    }
                    else
                    {
                        lastVisited = s.Pop();
                        r.Add(lastVisited.val);
                    }
                }
            }

            return(r);
        }
示例#2
0
        public void Flatten()
        {
            var root = new TreeNodeIb(3)
            {
                left = new TreeNodeIb(1), right = new TreeNodeIb(3)
            };

            if (root == null)
            {
                return;
            }

            var node = root;

            while (node != null)
            {
                // Attatches the right sub-tree to the rightmost leaf of the left sub-tree:
                if (node.left != null)
                {
                    var rightMost = node.left;
                    while (rightMost.right != null)
                    {
                        rightMost = rightMost.right;
                    }
                    rightMost.right = node.right;

                    // Makes the left sub-tree to the right sub-tree:
                    node.right = node.left;
                    node.left  = null;
                }

                // Flatten the rest of the tree:
                node = node.right;
            }
        }
示例#3
0
        public void Flatten2()
        {
            var a = new TreeNodeIb(3)
            {
                left = new TreeNodeIb(1), right = new TreeNodeIb(5)
            };
            var s = new Stack <TreeNodeIb>();
            var p = a;

            while (p != null || s.Count > 0)
            {
                if (p.right != null)
                {
                    s.Push(p.right);
                }

                if (p.left != null)
                {
                    p.right = p.left;
                    p.left  = null;
                }
                else
                {
                    if (s.Count > 0)
                    {
                        var tmp = s.Pop();
                        p.right = tmp;
                    }
                }
                p = p.right;
            }
        }
示例#4
0
        private bool hasSum(TreeNodeIb root, int sum)
        {
            if (root == null)
            {
                return(sum == 0);
            }

            var subSum = sum - root.val;

            if (subSum == 0 && root.left == null && root.right == null)
            {
                return(true);
            }

            var r = false;

            if (root.left != null)
            {
                r = r || hasSum(root.left, subSum);
            }
            if (root.right != null)
            {
                r = r || hasSum(root.right, subSum);
            }

            return(r);
        }
示例#5
0
        private void inorder(TreeNodeIb root)
        {
            if (root == null)
            {
                return;
            }

            inorder(root.left);

            if (pre == null)
            {
                pre = root;
            }
            else
            {
                if (root.val < pre.val)
                {
                    if (first == null)
                    {
                        first = pre;
                    }

                    second = root;
                }

                pre = root;
            }
            inorder(root.right);
        }
        private TreeNodeIb NonRecInvert(TreeNodeIb r)
        {
            var q = new Queue <TreeNodeIb>();

            if (r != null)
            {
                q.Enqueue(r);
            }

            while (q.Count > 0)
            {
                var p = q.Dequeue();
                if (p.left != null)
                {
                    q.Enqueue(p.left);
                }
                if (p.right != null)
                {
                    q.Enqueue(p.right);
                }

                var t = p.left;
                p.left  = p.right;
                p.right = t;
            }

            return(r);
        }
示例#7
0
        public int SumNumbers(TreeNodeIb root)
        {
            var result = 0;

            if (root == null)
            {
                return(result);
            }

            var res = new List <List <TreeNodeIb> >();
            var cur = new List <TreeNodeIb>();

            cur.Add(root);
            DFS(root, cur, res);

            foreach (var nodes in res)
            {
                var sb = new StringBuilder();
                foreach (var node in nodes)
                {
                    sb.Append(node.val.ToString());
                }
                var currValue = int.Parse(sb.ToString());
                result = result + currValue;
            }

            return(result);
        }
        public int T2Sum()
        {
            var root = new TreeNodeIb(10)
            {
                right = new TreeNodeIb(20), left = new TreeNodeIb(9)
            };
            var sum   = 19;
            var sumIb = new Bst(root);

            var x1 = sumIb.Left();
            var x2 = sumIb.Right();

            while (x1 < x2)
            {
                if (x1 + x2 == sum)
                {
                    return(1);
                }
                if (x1 + x2 < sum)
                {
                    x1 = sumIb.Left();
                }
                else
                {
                    x2 = sumIb.Right();
                }
            }
            return(0);
        }
        public List <int> Test(TreeNodeIb a)
        {
            if (a == null)
            {
                return(null);
            }
            var s = new Stack <TreeNodeIb>();
            var r = new List <int>();

            s.Push(a);

            while (s.Count > 0)
            {
                var p = s.Pop();
                r.Add(p.val);
                if (p.right != null)
                {
                    s.Push(p.right);
                }
                if (p.left != null)
                {
                    s.Push(p.left);
                }
            }

            return(r);
        }
        public int IsBalanced()
        {
            var a = new TreeNodeIb(1);
            var r = IsBalanced1(a);

            return(r ? 1 : 0);
        }
示例#11
0
        /// <summary>
        ///     max heap
        /// </summary>
        /// <param name="a"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        private TreeNodeIb build(List <int> a, int start, int end)
        {
            if (start > end)
            {
                return(null);
            }

            var minIndex = -1;
            var min      = int.MinValue;

            for (var i = start;
                 i <= end;
                 i++)
            {
                if (a[i] > min)
                {
                    minIndex = i;
                    min      = a[i];
                }
            }

            var n = new TreeNodeIb(a[minIndex]);

            n.left  = build(a, start, minIndex - 1);
            n.right = build(a, minIndex + 1, end);

            return(n);
        }
 private int GetHeight(TreeNodeIb root)
 {
     if (root == null)
     {
         return(-1);
     }
     return(1 + Math.Max(GetHeight(root.right), GetHeight(root.left)));
 }
 // efficient
 public int lcaEff(TreeNodeIb a, int val1, int val2)
 {
     a = findLCA(a, val1, val2);
     if (a == null)
     {
         return(-1);
     }
     return(a.val);
 }
示例#14
0
        public List <List <int> > ZigzagLevelOrder(TreeNodeIb a)
        {
            if (a == null)
            {
                return(null);
            }
            var res = new List <List <int> >();

            var q = new Queue <TreeNodeIb>();

            q.Enqueue(a);
            q.Enqueue(null); // sentinal

            var dir = false; // dir = false => add on back if dir == true then add on front

            var cur = new LinkedList <int>();

            while (q.Count > 0)
            {
                var p = q.Dequeue();

                if (p == null)
                {
                    dir = !dir;

                    if (q.Count > 0)
                    {
                        q.Enqueue(null);
                    }

                    res.Add(new List <int>(cur));
                    cur.Clear();
                    continue;
                }

                if (p.left != null)
                {
                    q.Enqueue(p.left);
                }

                if (p.right != null)
                {
                    q.Enqueue(p.right);
                }

                if (dir)
                {
                    cur.AddFirst(p.val);
                }
                else
                {
                    cur.AddLast(p.val);
                }
            }

            return(res);
        }
        private int getHeight(TreeNodeIb r)
        {
            if (r == null)
            {
                return(0);
            }

            return(1 + Math.Max(getHeight(r.left), getHeight(r.right)));
        }
示例#16
0
        public int SumNumbers(TreeNodeIb a)
        {
            if (a == null)
            {
                return(0);
            }

            return(DFS(a, 0, 0) % 1003);
        }
        public int MaxDepth(TreeNodeIb a)
        {
            if (a == null)
            {
                return(0);
            }

            return(getHeight(a));
        }
示例#18
0
        public int HasPathSum(TreeNodeIb root, int sum)
        {
            if (root == null)
            {
                return(0);
            }

            var r = hasSum(root, sum);

            return(r ? 1 : 0);
        }
        public void LcaTest1()
        {
            var node = new TreeNodeIb(3);

            node.left  = new TreeNodeIb(5);
            node.right = new TreeNodeIb(1);

            var r = lca(node, 5, 1);

            Assert.IsTrue(r == 3);
        }
        public void LcaTest2()
        {
            var node = new TreeNodeIb(3)
            {
                left  = new TreeNodeIb(5),
                right = new TreeNodeIb(1)
            };

            var r = lcaEff(node, 5, 1);

            Assert.IsTrue(r == 3);
        }
示例#21
0
        public List <int> RecoverTree(TreeNodeIb a)
        {
            inorder(a);

            var list = new List <int>();

            if (first != null && second != null)
            {
                list.Add(second.val);
                list.Add(first.val);
            }
            return(list);
        }
        public void Main()
        {
            var root = new TreeNodeIb(2)
            {
                left = new TreeNodeIb(1)
            };

            var i = new BstIterator(root);

            while (i.HasNext())
            {
                Console.WriteLine(i.HasNext());
            }
        }
        private bool IsBalanced1(TreeNodeIb a)
        {
            if (a == null)
            {
                return(true);
            }

            var lh = GetHeight(a.left);
            var rh = GetHeight(a.right);

            var abs1 = Math.Abs(lh - rh);

            var r = abs1 <= 1;

            return(r && IsBalanced1(a.left) && IsBalanced1(a.right));
        }
        public List <List <int> > PathSum(TreeNodeIb root, int sum)
        {
            var r = new List <List <int> >();

            if (root == null)
            {
                return(r);
            }

            var cur = new List <int>();

            cur.Add(root.val);
            ps(root, r, cur, sum - root.val);

            return(r);
        }
        public TreeNodeIb Invert(TreeNodeIb r)
        {
            if (r == null)
            {
                return(null);
            }

            r.left  = Invert(r.left);
            r.right = Invert(r.right);

            var t = r.left;

            r.left  = r.right;
            r.right = t;

            return(r);
        }
示例#26
0
        private static int DFS(TreeNodeIb a, int num, int sum)
        {
            if (a == null)
            {
                return(sum % 1003);
            }

            num = (num * 10 + a.val) % 1003;

            if (a.left == null && a.right == null)
            {
                sum = (sum + num) % 1003;
                return(sum % 1003);
            }

            sum = (sum + DFS(a.left, num, sum) + DFS(a.right, num, sum)) % 1003;
            return(sum % 1003);
        }
示例#27
0
        private TreeNodeIb sortedArrayToBSTRec(List <int> a, int start, int finish)
        {
            if (start > finish)
            {
                return(null);
            }

            var m = start + (finish - start) / 2;

            var left  = sortedArrayToBSTRec(a, start, m - 1);
            var root  = new TreeNodeIb(a[m]);
            var right = sortedArrayToBSTRec(a, m + 1, finish);

            root.left  = left;
            root.right = right;

            return(root);
        }
        private static TreeNodeIb Build(List <int> inOrder, List <int> postOrder, int inStart, int inEnd,
                                        int postStart, int postEnd)
        {
            if (inStart > inEnd || postStart > postEnd)
            {
                return(null);
            }

            var node = new TreeNodeIb(postOrder[postEnd]);

            var findIndex = Find(inOrder, inStart, inEnd, node.val);

            node.left = Build(inOrder, postOrder, inStart, findIndex - 1,
                              postStart, postStart + findIndex - (inStart + 1));
            node.right = Build(inOrder, postOrder, findIndex + 1, inEnd, postStart + findIndex - inStart,
                               postEnd - 1);

            return(node);
        }
 private static bool FindPath(TreeNodeIb node, List <int> path, int k)
 {
     if (node == null)
     {
         return(false);
     }
     path.Add(node.val);
     if (node.val == k)
     {
         return(true);
     }
     if (node.left != null && FindPath(node.left, path, k) ||
         node.right != null && FindPath(node.right, path, k))
     {
         return(true);
     }
     path.RemoveAt(path.Count - 1);
     return(false);
 }
示例#30
0
        public int Kthsmallest(TreeNodeIb a, int b)
        {
            var s = new Stack <TreeNodeIb>();

            // add sentinal
            s.Push(null);

            //move to left extremen (minimum)
            var t = a;

            while (t != null)
            {
                s.Push(t);
                t = t.left;
            }

            TreeNodeIb res = null;

            while (s.Count > 0)
            {
                res = s.Pop();

                b--;
                if (b == 0)
                {
                    break;
                }

                if (res.right != null)
                {
                    /* push the left subtree of right subtree */
                    var tmp = res.right;
                    while (tmp != null)
                    {
                        s.Push(tmp);
                        tmp = tmp.left;
                    }
                }
            }

            return(res != null ? res.val : -1);
        }