想不明白为什么这样做只能通过30%的案例,中序遍历然后判断list是否对称

对称的二叉树

http://www.nowcoder.com/questionTerminal/ff05d44dfdb04e1d83bdbdab320efbcb

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
    this.val = val;

}

}
*/
import java.util.ArrayList;
public class Solution {
static ArrayList<treenode>list=new ArrayList<>();</treenode>

boolean isSymmetrical(TreeNode pRoot)
{
    order(pRoot);
    for(int i=0;i<list.size()/2;i++){
        if(list.get(i).val!=list.get(list.size()-1-i).val){
            return false;
        }
    }
    return true;


}
public void order(TreeNode pRoot){
    if(pRoot!=null){
        order(pRoot.left);
        list.add(pRoot);
        order(pRoot.right);
    }
}

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
09-17 09:40
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务