static private void FindNextNode(TreeNode257 fatherNode, ref List <List <int> > returnList, List <int> fatherData) { List <int> temp = new List <int>(); temp = fatherData.ToList(); temp.Add(fatherNode.val); List <int> leftFatherData = temp; List <int> rightFatherData = temp; //结束条件 if (fatherNode.left == null && fatherNode.right == null) { returnList.Add(temp); return; } if (fatherNode.left != null) { FindNextNode(fatherNode.left, ref returnList, leftFatherData); } if (fatherNode.right != null) { FindNextNode(fatherNode.right, ref returnList, rightFatherData); } }
static private List <List <int> > PrintAllPath(TreeNode257 rootNode) { List <List <int> > returnList = new List <List <int> >(); List <int> fatherData = new List <int>(); FindNextNode(rootNode, ref returnList, fatherData); return(returnList); }
static public IList <string> BinaryTreePaths(TreeNode257 root) { IList <string> result = new List <string>(); if (root == null) { return(null); } List <List <int> > resultList = PrintAllPath(root); foreach (List <int> l in resultList) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < l.Count; j++) { sb.Append(l[j] + "->"); } result.Add(sb.Remove(sb.Length - 2, 2).ToString()); } return(result); }