码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode988

时间:2019-02-05 23:40:35      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:solution   index   pop   i++   var   .com   new   ==   return   

 1 public class Solution
 2     {
 3         private Stack<string> ST = new Stack<string>();
 4         private string SmallestStr = String.Empty;
 5         private string[] Ary = new string[] { "a","b","c","d","e","f","g",
 6         "h","i","j","k","l","m","n","o","p","q","r","s","t",
 7             "u","v","w","x","y","z"};
 8         private void SearchTree(TreeNode root)
 9         {
10             if(root!=null)
11             {
12                 var index = root.val;
13                 var str = Ary[index];
14                 ST.Push(str);
15 
16                 if(root.left!=null)
17                 {
18                     SearchTree(root.left);
19                 }
20 
21                 if(root.right!=null)
22                 {
23                     SearchTree(root.right);
24                 }
25                 if(root.left==null && root.right==null)
26                 {
27                     //find a leaf node
28                     var a1 = ST.ToArray();
29                     StringBuilder sb1 = new StringBuilder();
30                     for (int i = 0; i < a1.Length;i++)
31                     {
32                         sb1.Append(a1[i]);
33                     }
34                     if(string.IsNullOrEmpty(SmallestStr) || 
35                        string.Compare(SmallestStr, sb1.ToString()) > 0
36                       )
37                     {
38                         SmallestStr = sb1.ToString();
39                     }
40                 }
41                 ST.Pop();
42             }
43         }
44 
45         public string SmallestFromLeaf(TreeNode root)
46         {
47             SearchTree(root);
48             return SmallestStr;
49         }
50     }

 

leetcode988

标签:solution   index   pop   i++   var   .com   new   ==   return   

原文地址:https://www.cnblogs.com/asenyang/p/10353261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!