public int KthSmallest(TreeNode root, int k)
        {
            Dictionary<TreeNode, int> map = new Dictionary<TreeNode, int>();
            Recur(root, map);

            var curnode = root;
            while(true)
            {

                if (curnode.left != null)
                {
                    if (k > map[curnode.left])
                    {
                        k -= map[curnode.left];
                        if (k == 1)
                            return curnode.val;
                        k--;
                        curnode = curnode.right;
                    }
                    else
                    {
                        curnode = curnode.left;
                    }
                }
                else
                {
                    if (k == 1)
                        return curnode.val;
                    k--;
                    curnode = curnode.right;
                }
            }
        }
        public int KthSmallest3(TreeNode root, int k)
        {
            Stack<TreeNode> st = new Stack<TreeNode>();

            var tmp = root;
            while(tmp != null)
            {
                st.Push(tmp);
                tmp = tmp.left;
            }

            while(st.Count() > 0)
            {
                var cur = st.Pop();
                k--;
                if (k == 0)
                    return cur.val;

                if (cur.right != null)
                {
                    tmp = cur.right;
                    while(tmp != null)
                    {
                        st.Push(tmp);
                        tmp = tmp.left;
                    }
                }
            }

            return 0;
        }
        bool checkBST(TreeNode node, Stack<int> lst, Stack<int> rst)
        {
            bool re = true;

            if (lst.Count != 0)
            {
                int lval = lst.Peek();
                if (node.val > lval)
                    return false;
            }
            if (rst.Count != 0)
            {
                int rval = rst.Peek();
                if (node.val < rval)
                    return false;
            }

            if (node.left != null)
            {
                lst.Push(node.val);
                if(!checkBST(node.left, lst, rst))
                    return false;
                lst.Pop();
            }

            if (node.right != null)
            {
                rst.Push(node.val);
                if (!checkBST(node.right, lst, rst))
                    return false;
                rst.Pop();
            }

            return re;
        }
        public TreeNode SortedListToBST(ListNode head)
        {
            if (head == null)
                return null;

            if (head.next == null)
                return new TreeNode(head.val);

            ListNode p = head;
            ListNode q = head;
            ListNode pre = q;
            while(p != null && p.next != null)
            {
                pre = q;
                q = q.next;
                p = p.next.next;
            }

            TreeNode tn = new TreeNode(q.val);
            if (q == head)
            {
                tn.right = new TreeNode(q.next.val);
            }
            else
            {
                tn = new TreeNode(q.val);
                pre.next = null;
                tn.left = SortedListToBST(head);
                tn.right = SortedListToBST(q.next);                
            }

            return tn;
        }
        public int SumNumbers(TreeNode root)
        {
            Stack<TreeNode> st = new Stack<TreeNode>();
            Stack<int> vst = new Stack<int>();
            if (root == null)
                return 0;

            int re = 0;

            st.Push(root);
            vst.Push(0);

            while (st.Count != 0)
            {
                TreeNode cur = st.Pop();
                int cval = vst.Pop();
                

                if (cur.left == null && cur.right == null)
                    re += cval + cur.val;

                if (cur.left != null)
                {
                    st.Push(cur.left);
                    vst.Push((cval + cur.val) * 10);
                }
                if (cur.right != null)
                {
                    st.Push(cur.right);
                    vst.Push((cval + cur.val) * 10);
                }
            }
            return re;
        }
        public IList<int> InorderTraversal(TreeNode root)
        {
            var re = new List<int>();
            Recur(root, re);

            return re;
        }
        public static void Run()
        {
            var st = new Solution98();
            Console.WriteLine("Start: {0}", DateTime.Now);

            TreeNode tn = new TreeNode(3);
            TreeNode tn1 = new TreeNode(1);
            TreeNode tn2 = new TreeNode(8);
            TreeNode tn3 = new TreeNode(4);
            TreeNode tn4 = new TreeNode(5);
            TreeNode tn5 = new TreeNode(6);
            TreeNode tn6 = new TreeNode(7);

            tn.left = tn1;
            tn.right = tn3;

            tn1.left = null;
            tn1.right = tn2;

            tn3.left = null;
            tn3.right = tn4;

            tn4.left = null;
            tn4.right = tn5;

            tn5.left = null;
            tn5.right = tn6;

            var re = st.IsValidBST(tn);
            Console.WriteLine(re);

            Console.WriteLine("End: {0}", DateTime.Now);
        }
        public bool HasPathSum(TreeNode root, int sum)
        {
            Queue<Tuple<TreeNode, int>> q = new Queue<Tuple<TreeNode, int>>();

            if (root == null)
                return false;

            q.Enqueue(new Tuple<TreeNode, int>(root, root.val));
            while(q.Count > 0)
            {
                var item = q.Dequeue();
                var node = item.Item1;
                var ts = item.Item2;

                if (node.left == null && node.right == null && ts == sum)
                    return true;

                if (node.left != null)
                    q.Enqueue(new Tuple<TreeNode, int>(node.left, ts + node.left.val));

                if (node.right != null)
                    q.Enqueue(new Tuple<TreeNode, int>(node.right, ts + node.right.val));
            }
            return false;
        }
        public static void Run()
        {
            var st = new Solution103();

            Console.WriteLine("Start: {0}", DateTime.Now);

            TreeNode tn = new TreeNode(3);
            tn.left = new TreeNode(9);
            tn.right = new TreeNode(20);
            tn.right.left = new TreeNode(15);
            tn.right.right = new TreeNode(7);

            var re = st.ZigzagLevelOrder(tn);
            foreach (var lt in re)
            {
                foreach (var data in lt)
                    Console.Write("{0}  ", data);
                Console.WriteLine();
            }

            

            Console.WriteLine("End: {0}", DateTime.Now);

        }
        public IList<int> PreorderTraversal(TreeNode root)
        {
            var re = new List<int>();
            if (root == null)
                return re;

            var st = new Stack<TreeNode>();

            st.Push(root);
            re.Add(root.val);
            while (root.left != null)
            {
                root = root.left;
                st.Push(root);
                re.Add(root.val);
            }
            while (st.Count() > 0)
            {
                var node = st.Pop();

                var right = node.right;

                while (right != null)
                {
                    st.Push(right);
                    re.Add(right.val);
                    right = right.left;
                }
            }

            return re;
        }
        public IList<IList<int>> PathSum(TreeNode root, int sum)
        {
            List<IList<int>> list = new List<IList<int>>();
            if (root == null)
                return list;

            Queue<Tuple<TreeNode, int, List<int>>> st = new Queue<Tuple<TreeNode, int, List<int>>>();
            st.Enqueue(new Tuple<TreeNode, int, List<int>> ( root, sum, new List<int>() ));
            while(st.Count > 0)
            {
                var item = st.Dequeue();
                var tn = item.Item1;
                var sn = item.Item2;
                var cl = item.Item3;

                cl.Add(tn.val);
                if (tn.left == null && tn.right == null && tn.val == sn)
                {
                    list.Add(cl);
                }
                if (tn.left != null)
                {
                    st.Enqueue(new Tuple<TreeNode, int, List<int>>(tn.left, sn - tn.val, cl));
                    if (tn.right != null)
                        st.Enqueue(new Tuple<TreeNode, int, List<int>>(tn.right, sn - tn.val, new List<int>(cl)));
                }
                else if (tn.right != null)
                {
                    st.Enqueue(new Tuple<TreeNode, int, List<int>>(tn.right, sn - tn.val, cl));
                }
            }

            
            return list;
        }
Beispiel #12
0
        public static void Run()
        {
            var st = new Solution99();

            Console.WriteLine("Start: {0}", DateTime.Now);

            /*
                         4
                       2   3
                      1 5
            */
            var tn = new TreeNode(4);
            tn.left = new TreeNode(2);
            tn.right = new TreeNode(3);
            tn.left.left = new TreeNode(1);
            tn.left.right = new TreeNode(5);
            st.RecoverTree(tn);
            Util.PrintTreeNode(tn);

            var tn2 = new TreeNode(0);
            tn2.left = new TreeNode(1);
            st.RecoverTree(tn2);
            Util.PrintTreeNode(tn2);

            Console.WriteLine("End: {0}", DateTime.Now);
        }
        public IList<IList<int>> LevelOrderUp(TreeNode root)
        {
            var q = new Queue<Tuple<TreeNode, int>>();
            var list = new List<IList<int>>();

            if (root == null)
                return list;

            q.Enqueue(new Tuple<TreeNode, int>(root, 0));
            while(q.Count > 0)
            {
                var item = q.Dequeue();
                var node = item.Item1;
                var level = item.Item2;
                if (node.left != null)
                    q.Enqueue(new Tuple<TreeNode, int>(node.left, level + 1));
                if (node.right != null)
                    q.Enqueue(new Tuple<TreeNode, int>(node.right, level + 1));

                if (list.Count <= level)
                    list.Add(new List<int>() { node.val });
                else
                    list[level].Add(node.val);
            }

            return list;
        }
        TreeNode Recur(int[] inorder, int pb, int pe,  Dictionary<int, int> map)
        {
            if (pb == pe)
                return new TreeNode(inorder[pb]);

            int min = map[inorder[pb]];
            int pos = pb;
            for(int i = pb+1; i<= pe; i++)
            {
                if (map[inorder[i]] < min)
                {
                    min = map[inorder[pb]];
                    pos = i;
                }                    
            }
            var tn = new TreeNode(inorder[pos]);
            
            if (pos == pb)
                tn.left = null;
            else
            {
                tn.left = Recur(inorder, pb, pos-1, map);
            }

            if (pos == pe)
                tn.right = null;
            else
                tn.right = Recur(inorder, pos + 1, pe, map);

            return tn;
        }
Beispiel #15
0
 public bool Traverse(TreeNode root, int sum)
 {
     if (root == null)
     {
         return false;
     }
     /*代表是叶子节点*/
     if (root.left == null && root.right == null)
     {
         pathSum = pathSum + root.val;
         if (pathSum == sum)
             return true;
         else
         {
             pathSum = pathSum - root.val;
             return false;
         }
     }
     pathSum = pathSum + root.val;
     /*如果左右节点都返回false,则将这个节点去除,并返回false*/
     if (false == Traverse(root.left, sum) && false == Traverse(root.right, sum))
     {
         pathSum = pathSum - root.val;
         return false;
     }
     else
     {
         return true;//左右节点只要有一个节点返回true,则就返回true
     }
 }
        public static void Run()
        {
            var st = new Solution235();

            TreeNode tn = new TreeNode(6);
            TreeNode tn2 = new TreeNode(2);
            TreeNode tn3 = new TreeNode(8);
            TreeNode tn4 = new TreeNode(0);
            TreeNode tn5 = new TreeNode(4);
            TreeNode tn6 = new TreeNode(7);
            TreeNode tn7 = new TreeNode(9);
            tn4.left = tn4.right = null;
            tn5.left = tn5.right = null;
            tn6.left = tn6.right = null;
            tn7.left = tn7.right = null;
            tn.left = tn2;
            tn.right = tn3;
            tn2.left = tn4;
            tn2.right = tn5;
            tn3.left = tn6;
            tn3.right = tn7;

            Console.WriteLine("Start: {0}", DateTime.Now);
            var re = st.LowestCommonAncestor(tn, tn5, tn7);
            Console.WriteLine("{0}", re.val);

            Console.WriteLine("End: {0}", DateTime.Now);
            
        }
        public static void Run()
        {
            var st = new Solution226();

            Console.WriteLine("Start: {0}", DateTime.Now);

            TreeNode tn = new TreeNode(1);
            TreeNode tn2 = new TreeNode(2);
            TreeNode tn3 = new TreeNode(3);
            TreeNode tn4 = new TreeNode(4);
            TreeNode tn5 = new TreeNode(5);
            TreeNode tn6 = new TreeNode(6);
            TreeNode tn7 = new TreeNode(7);
            tn4.left = tn4.right = null;
            tn5.left = tn5.right = null;
            tn6.left = tn6.right = null;
            tn7.left = tn7.right = null;
            tn.left = tn2;
            tn.right = tn3;
            tn2.left = tn4;
            tn2.right = tn5;
            tn3.left = tn6;
            tn3.right = tn7;

            Util.PrintTreeNode(st.InvertTree(tn));

            Console.WriteLine("End: {0}", DateTime.Now);

        }
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
        {
            Dictionary<TreeNode, List<TreeNode>> dict = new Dictionary<TreeNode, List<TreeNode>>();

            Stack<TreeNode> st = new Stack<TreeNode>();
            st.Push(root);
            var lt = new List<TreeNode>();
            lt.Add(root);
            dict.Add(root, lt);

            var pfound = false;
            var qfound = false;
            
            while(st.Count > 0)
            {
                TreeNode cur = st.Pop();

                if (cur == p)
                    pfound = true;
                if (cur == q)
                    qfound = true;

                if (pfound && qfound)
                    break;

                var curList = dict[cur];

                if (cur.left != null)
                {
                    var lnode = new List<TreeNode>();
                    lnode.AddRange(curList);
                    lnode.Add(cur.left);
                    dict.Add(cur.left, lnode);
                    st.Push(cur.left);
                }
                if (cur.right != null)
                {
                    var rnode = new List<TreeNode>();
                    rnode.AddRange(curList);
                    rnode.Add(cur.right);
                    dict.Add(cur.right, rnode);
                    st.Push(cur.right);
                }
            }

            var plist = dict[p];
            var qlist = dict[q];
            int i = 0;
            for(; i< plist.Count() && i < qlist.Count(); i++)
            {
                if (plist[i] != qlist[i])
                    break;
            }
            if (i == plist.Count())
                return p;
            if (i == qlist.Count())
                return q;
            return plist[i - 1];
        }
        public IList<IList<int>> LevelOrder(TreeNode root)
        {
            var list = new List<IList<int>>();

            Traverse(root, list, 0);

            return list;
        }
        public bool IsSymmetric(TreeNode root)
        {
            if (root == null)
                return true;

            return Judge(root.left, root.right);

        }
Beispiel #21
0
        public bool IsValidBST(TreeNode root)
        {
            Stack<int> leftst = new Stack<int>();
            Stack<int> rightst = new Stack<int>();
            if (root == null)
                return true;

            return checkBST(root, leftst, rightst);
        }
 public BSTIterator(TreeNode root)
 {
     lt = new Stack<TreeNode>();
     while(root != null)
     {
         lt.Push(root);
         root = root.left;
     }
 }
        public int MaxDepth(TreeNode root)
        {
            if (root == null)
                return 0;

            var left = MaxDepth(root.left) + 1;
            var right = MaxDepth(root.right) + 1;
            return left > right ? left : right;
        }
        public int MaxPathSum(TreeNode root)
        {
            if (root == null)
                return 0;

            int re = int.MinValue;
            Recur(root, ref re);
            return re;
        }
        public IList<IList<int>> PathSum2(TreeNode root, int sum)
        {
            List<IList<int>> list = new List<IList<int>>();
            if (root == null)
                return list;

            PathFindSum(root, sum, list, new List<int>());

            return list;
        }
        public IList<IList<int>> LevelOrderBottom(TreeNode root)
        {
            var list = LevelOrderUp(root);
            var nlist = new List<IList<int>>();

            for (int i = list.Count - 1; i >= 0; i--)
                nlist.Add(list[i]);

            return nlist;
        }
        public int MinDepth(TreeNode root)
        {
            int level = -1;

            if (root == null)
                return 0;

            Traverse(root, 1, ref level);
            return level;
        }
        public void Recur(TreeNode tn, int level, Dictionary<int, int> map)
        {
            if (tn.right != null)
                Recur(tn.right, level + 1, map);

            if (!map.ContainsKey(level))
                map[level] = tn.val;

            if (tn.left != null)
                Recur(tn.left, level + 1, map);
        }
        public static void Run()
        {
            var st = new Solution129();
            Console.WriteLine("Start: {0}", DateTime.Now);

            TreeNode p = new TreeNode(1);
            p.left = new TreeNode(2);
            p.right = new TreeNode(3);
            Console.WriteLine(st.SumNumbers(p));
            Console.WriteLine("End: {0}", DateTime.Now);
        }
        public bool IsSameTree(TreeNode p, TreeNode q)
        {
            if (p == null && q == null)
                return true;
            if ((p == null && q != null) || (p != null && q == null))
                return false;

            if (p.val != q.val)
                return false;

            return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right);
        }