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

101. Symmetric Tree

时间:2018-09-20 11:14:18      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:http   build   class   new   div   tps   nbsp   span   end   

https://leetcode.com/problems/symmetric-tree/discuss/166375/Java-Accepted-very-simple-solution

 

 1 class Solution {
 2     StringBuilder sb1 = new StringBuilder();
 3     StringBuilder sb2 = new StringBuilder();
 4     public boolean isSymmetric(TreeNode root) {
 5         if(root == null) return true;
 6         leftR(root);
 7         rightR(root);
 8         if(sb1.toString().equals(sb2.toString())) {
 9             return true;
10         }else {
11             return false;
12         }
13         
14     }
15    
16     public void leftR(TreeNode root) {
17         if(root != null) {
18             leftR(root.left);
19             sb1.append("l");
20             sb1.append(root.val);
21             leftR(root.right);
22             sb1.append("r");
23         }
24             
25         
26  }
27     
28     public void rightR(TreeNode root) {
29         if(root != null) {
30             rightR(root.right);
31             sb2.append("r");
32             sb2.append(root.val);
33             rightR(root.left);
34             sb2.append("l");
35         }
36     }   
37 }

 

101. Symmetric Tree

标签:http   build   class   new   div   tps   nbsp   span   end   

原文地址:https://www.cnblogs.com/goPanama/p/9678605.html

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