[LeetCode]331. Verify Preorder Serialization of a Binary Tree

来源:互联网 发布:python检测ip能否ping 编辑:程序博客网 时间:2024/05/01 07:43

Problem Description

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 #.
[https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/]

思路

没啥好说的,遇到俩#就弹出俩然后把栈顶的数换成#,如果栈空就说明是对的

Code

package q331;import java.util.Stack;public class Solution {    public boolean isValidSerialization(String preorder) {        Stack<String> s = new Stack<String>();        if (preorder.length() <= 1 && !preorder.equals("#"))            return false;        if (preorder.charAt(0) == '#' && preorder.length() > 1)            return false;        String b;        String[] str = preorder.split(",");        s.push(str[0]);        for (int i = 1; i < str.length; i++) {            b = str[i];            while (s.peek().equals("#") && b.equals("#")) {                s.pop();                if (s.isEmpty()) {                    if (i != str.length - 1)                        return false;                    break;                }                if (!s.peek().equals("#")) {                    s.pop();                    if (s.isEmpty()) {                        if (i != str.length - 1)                            return false;                        break;                    }                    b = s.peek();                    s.push("#");                }            }            if (b.equals(str[i]))                s.push(b);        }        while (!s.isEmpty() && s.peek().equals("#"))            s.pop();        return s.isEmpty();    }//  public static void main(String[] args) {//      Solution s = new Solution();//      // System.out.println(s.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));//      // System.out.println(s.isValidSerialization("9,#,#,1"));//      System.out.println(s.isValidSerialization("9,#,92,#,#"));//  }}
0 0