至此2016-2-15,全部333道题除去锁题和不想做的= =。就都做完了。。。
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #
.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#"
, where #
represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#'
representing null
pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3"
.
Example 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
Return true
Example 2:
"1,#"
Return false
Example 3:
"9,#,#,1"
Return false
判断先序遍历能否组成一棵树
运用正则表达式,然后一点一点“砍叶”,因为不全的地方包括左右子节点都会用#代替,那么1-9,#,#就是一个叶节点,我们把尾端每个叶节点都用#代替,下面一层叶节点就没了,反复几次,知道没有1-9,#,#叶节点了,如果最后剩的是#空的根,就说明节点全部砍光,就是一个二叉树,否则不是
public class Solution { public bool IsValidSerialization(string preorder) { var t = String.Empty; preorder = System.Text.RegularExpressions.Regex.Replace(preorder, "[0-9]+", "0"); while (preorder != t) { t = preorder; preorder = preorder.Replace("0,#,#", "#"); } return preorder == "#"; } }
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to]
, reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK
. Thus, the itinerary must begin with JFK
.
Note:
["JFK", "LGA"]
has a smaller lexical order than ["JFK", "LGB"]
.Example 1:
tickets
= [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"]
.
Example 2:
tickets
= [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"]
.
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]
. But it is larger in lexical order.
最简路径
博主开始的代码可以求出最简,但是顺序不对,难道这个不是有多种可能性的吗。。。不知道原因
参考:
https://leetcode.com/discuss/86271/c%23-recursive-solution
public class Solution { private IDictionary<string, List<string>> m_routes; private int m_count; public IList<string> FindItinerary(string[,] tickets) { m_routes = new Dictionary<string, List<string>>(); m_count = tickets.GetLength(0); for (int i = 0; i < m_count; i++) { var s = tickets[i, 0]; var d = tickets[i, 1]; if (!m_routes.ContainsKey(s)) m_routes.Add(s, new List<string>()); m_routes[s].Add(d); } foreach (var list in m_routes.Values) list.Sort(); return GetPath("JFK"); } private IList<string> GetPath(string s) { if (m_routes.ContainsKey(s) && m_routes[s].Count > 0) { var children = m_routes[s]; for (int i = 0; i < children.Count; i++) { var child = children[i]; children.RemoveAt(i); m_count--; var best = GetPath(child); children.Insert(i, child); m_count++; if (best != null) { best.Insert(0, s); return best; } } return null; } if (m_count == 0) return new List<string> {s}; return null; } }